using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
class ImageClip : Form {
MenuItem miCut, miCopy, miPaste, miDel;
Image image;
public static void Main() {
Application.Run(new ImageClip());
}
public ImageClip() {
Text = "Image Clip";
MenuItem mi = new MenuItem("&Edit");
mi.Popup += new EventHandler(MenuEditOnPopup);
Menu.MenuItems.Add(mi);
int index = Menu.MenuItems.Count - 1;
miCut = new MenuItem("Cu&t");
miCut.Click += new EventHandler(MenuEditCutOnClick);
miCut.Shortcut = Shortcut.CtrlX;
Menu.MenuItems[index].MenuItems.Add(miCut);
miCopy = new MenuItem("&Copy");
miCopy.Click += new EventHandler(MenuEditCopyOnClick);
miCopy.Shortcut = Shortcut.CtrlC;
Menu.MenuItems[index].MenuItems.Add(miCopy);
miPaste = new MenuItem("&Paste");
miPaste.Click += new EventHandler(MenuEditPasteOnClick);
miPaste.Shortcut = Shortcut.CtrlV;
Menu.MenuItems[index].MenuItems.Add(miPaste);
miDel = new MenuItem("De&lete");
miDel.Click += new EventHandler(MenuEditDelOnClick);
miDel.Shortcut = Shortcut.Del;
Menu.MenuItems[index].MenuItems.Add(miDel);
}
void MenuEditOnPopup(object obj, EventArgs ea) {
miCut.Enabled = miCopy.Enabled = miDel.Enabled = image != null;
IDataObject data = Clipboard.GetDataObject();
miPaste.Enabled = data.GetDataPresent(typeof(Bitmap)) || data.GetDataPresent(typeof(Metafile));
}
void MenuEditCutOnClick(object obj, EventArgs ea) {
MenuEditCopyOnClick(obj, ea);
MenuEditDelOnClick(obj, ea);
}
void MenuEditCopyOnClick(object obj, EventArgs ea) {
Clipboard.SetDataObject(image, true);
}
void MenuEditPasteOnClick(object obj, EventArgs ea) {
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(typeof(Metafile)))
image = (Image)data.GetData(typeof(Metafile));
else if (data.GetDataPresent(typeof(Bitmap)))
image = (Image)data.GetData(typeof(Bitmap));
Invalidate();
}
void MenuEditDelOnClick(object obj, EventArgs ea) {
image = null;
Invalidate();
}
}
GUI Windows Form
Clip Text
using System;
using System.Drawing;
using System.Windows.Forms;
class ClipText: Form
{
string strText = "Sample text";
MenuItem miCut, miCopy, miPaste;
public static void Main()
{
Application.Run(new ClipText());
}
public ClipText()
{
ResizeRedraw = true;
Menu = new MainMenu();
MenuItem mi = new MenuItem("&Edit");
mi.Popup += new EventHandler(MenuEditOnPopup);
Menu.MenuItems.Add(mi);
miCut = new MenuItem("Cu&t");
miCut.Click += new EventHandler(MenuEditCutOnClick);
miCut.Shortcut = Shortcut.CtrlX;
Menu.MenuItems[0].MenuItems.Add(miCut);
miCopy = new MenuItem("&Copy");
miCopy.Click += new EventHandler(MenuEditCopyOnClick);
miCopy.Shortcut = Shortcut.CtrlC;
Menu.MenuItems[0].MenuItems.Add(miCopy);
miPaste = new MenuItem("&Paste");
miPaste.Click += new EventHandler(MenuEditPasteOnClick);
miPaste.Shortcut = Shortcut.CtrlV;
Menu.MenuItems[0].MenuItems.Add(miPaste);
}
void MenuEditOnPopup(object obj, EventArgs ea)
{
miCut.Enabled = miCopy.Enabled = strText.Length > 0;
miPaste.Enabled = Clipboard.GetDataObject().GetDataPresent(typeof(string));
}
void MenuEditCutOnClick(object obj, EventArgs ea)
{
MenuEditCopyOnClick(obj, ea);
strText = "";
Invalidate();
}
void MenuEditCopyOnClick(object obj, EventArgs ea)
{
Clipboard.SetDataObject(strText, true);
}
void MenuEditPasteOnClick(object obj, EventArgs ea)
{
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(typeof(string)))
strText = (string) data.GetData(typeof(string));
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
StringFormat strfmt = new StringFormat();
strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
grfx.DrawString(strText, Font, new SolidBrush(ForeColor),
ClientRectangle, strfmt);
}
}
Copying from the Clipboard.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
public class MainClass {
public static void Main(string[] args) {
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text)) {
Console.WriteLine((String)iData.GetData(DataFormats.Text));
}
if (iData.GetDataPresent(DataFormats.Bitmap)) {
Image img = (Bitmap)iData.GetData(DataFormats.Bitmap);
}
}
}
Copying to the Clipboard.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
public class MainClass
{
public static void Main(string[] args)
{
Clipboard.SetDataObject("data");
}
}
CheckBox Style
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CheckBox
{
/// <summary>
/// Summary description for CheckBoxStyle.
/// </summary>
public class CheckBoxStyle : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.CheckBox chk_option1;
private System.Windows.Forms.CheckBox chk_option2;
private System.Windows.Forms.CheckBox chk_option3;
private System.Windows.Forms.CheckBox chk_option4;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public CheckBoxStyle()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.chk_option1 = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.chk_option2 = new System.Windows.Forms.CheckBox();
this.chk_option3 = new System.Windows.Forms.CheckBox();
this.chk_option4 = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// chk_option1
//
this.chk_option1.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
this.chk_option1.Location = new System.Drawing.Point(40, 24);
this.chk_option1.Name = "chk_option1";
this.chk_option1.Size = new System.Drawing.Size(192, 24);
this.chk_option1.TabIndex = 0;
this.chk_option1.Text = "Windows 2000 / Windows XP";
this.chk_option1.ThreeState = true;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(296, 16);
this.label1.TabIndex = 1;
this.label1.Text = "The following products are developed by Microsoft Corp.";
//
// chk_option2
//
this.chk_option2.Location = new System.Drawing.Point(40, 56);
this.chk_option2.Name = "chk_option2";
this.chk_option2.Size = new System.Drawing.Size(192, 24);
this.chk_option2.TabIndex = 0;
this.chk_option2.Text = "JAVA";
//
// chk_option3
//
this.chk_option3.Appearance = System.Windows.Forms.Appearance.Button;
this.chk_option3.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.chk_option3.Location = new System.Drawing.Point(40, 88);
this.chk_option3.Name = "chk_option3";
this.chk_option3.Size = new System.Drawing.Size(192, 24);
this.chk_option3.TabIndex = 0;
this.chk_option3.Text = "Visual C# and .NET";
//
// chk_option4
//
this.chk_option4.Appearance = System.Windows.Forms.Appearance.Button;
this.chk_option4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.chk_option4.Location = new System.Drawing.Point(40, 120);
this.chk_option4.Name = "chk_option4";
this.chk_option4.Size = new System.Drawing.Size(192, 24);
this.chk_option4.TabIndex = 0;
this.chk_option4.Text = "Oracle Database";
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.chk_option2,
this.chk_option4,
this.chk_option3,
this.chk_option1});
this.groupBox1.Location = new System.Drawing.Point(16, 56);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(280, 152);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Select All Correct Options";
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 216);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "Done ";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// CheckBoxStyle
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328, 253);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.groupBox1,
this.label1});
this.Name = "CheckBoxStyle";
this.Text = "Check Boxes";
this.Load += new System.EventHandler(this.CheckBoxStyle_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new CheckBoxStyle());
}
private void CheckBoxStyle_Load(object sender, System.EventArgs e)
{
// Load the Image that we want to display on the button
Image imga = Image.FromFile("WRENCH.ICO");
// Assign the Image for the checkbox
chk_option3.Image = imga;
// Align the image on the face of the checkbox
chk_option3.ImageAlign = ContentAlignment.MiddleRight;
// Select the background color for the checkbox
chk_option3.BackColor = Color.LightBlue ;
// Load the Image that we want to display on the button
imga = Image.FromFile("EYE.ICO");
// Assign the Image for the checkbox
chk_option4.Image = imga;
// Align the image on the face of the checkbox
chk_option4.ImageAlign = ContentAlignment.MiddleCenter;
// Select the background color for the checkbox
chk_option4.BackColor = Color.LightBlue ;
}
private void button1_Click(object sender, System.EventArgs e)
{
string str_opt_selected = "Options Selected are ..
" ;
// Check which options are selected for every checkbox
// using the Checked property
if ( chk_option1.Checked )
str_opt_selected = str_opt_selected + chk_option1.Text + "
";
if ( chk_option2.Checked )
str_opt_selected = str_opt_selected + chk_option2.Text + "
";
if ( chk_option3.Checked )
str_opt_selected = str_opt_selected + chk_option3.Text + "
";
if ( chk_option4.Checked )
str_opt_selected = str_opt_selected + chk_option4.Text + "
";
MessageBox.Show(str_opt_selected , "Checked Options");
}
}
}
CheckBox.zip( 33 k)
CheckButton on a Form
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CheckButton
{
/// <summary>
/// Summary description for FormCheckButton.
/// </summary>
public class FormCheckButton : System.Windows.Forms.Form
{
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.IContainer components;
public FormCheckButton()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
checkBox1.Appearance = Appearance.Button;
checkBox1.FlatStyle = FlatStyle.System;
checkBox1.TextAlign = ContentAlignment.MiddleCenter;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 32);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
//
// checkBox1
//
this.checkBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.checkBox1.Location = new System.Drawing.Point(72, 88);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(104, 32);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
//
// FormCheckButton
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 141);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.checkBox1});
this.Name = "FormCheckButton";
this.Text = "FormCheckButton";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FormCheckButton());
}
}
}
Is CheckBox checked
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public Form1() {
InitializeComponent();
}
private void InitializeComponent() {
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.button1 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.radioButton1,
this.radioButton2,
this.radioButton3});
this.groupBox1.Location = new System.Drawing.Point(8, 120);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(120, 144);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Color";
this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter);
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(8, 8);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "Circle";
//
// checkBox2
//
this.checkBox2.Location = new System.Drawing.Point(8, 40);
this.checkBox2.Name = "checkBox2";
this.checkBox2.TabIndex = 2;
this.checkBox2.Text = "Rectangle";
//
// checkBox3
//
this.checkBox3.Location = new System.Drawing.Point(8, 72);
this.checkBox3.Name = "checkBox3";
this.checkBox3.TabIndex = 3;
this.checkBox3.Text = "String";
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(8, 32);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 4;
this.radioButton1.Text = "Red";
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.Location = new System.Drawing.Point(8, 64);
this.radioButton2.Name = "radioButton2";
this.radioButton2.TabIndex = 5;
this.radioButton2.Text = "Green";
//
// radioButton3
//
this.radioButton3.Location = new System.Drawing.Point(8, 96);
this.radioButton3.Name = "radioButton3";
this.radioButton3.TabIndex = 6;
this.radioButton3.Text = "Blue";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 280);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 32);
this.button1.TabIndex = 4;
this.button1.Text = "Draw";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(408, 317);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.checkBox3,
this.checkBox2,
this.checkBox1,
this.groupBox1});
this.Name = "Form1";
this.Text = "CheckBox and RadioButton Sample";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void groupBox1_Enter(object sender, System.EventArgs e)
{
Console.WriteLine("group box enter event");
}
private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
{
Console.WriteLine("Radio Button checked changed event");
}
private void button1_Click(object sender, System.EventArgs e)
{
Graphics g = Graphics.FromHwnd(this.Handle);
String str = "";
Rectangle rc = new Rectangle(150, 50, 250, 250);
if(radioButton1.Checked)
{
str = "red";
}
if(radioButton2.Checked)
{
str+="Green";
}
if(radioButton3.Checked)
{
str+="Blue";
}
if (checkBox1.Checked)
{
str+="Ellipse";
}
if (checkBox2.Checked)
{
str += "Rectangle";
}
if (checkBox3.Checked)
{
g.FillRectangle(new SolidBrush(Color.White), rc);
g.DrawString(str, new Font("Verdana", 12), new SolidBrush(Color.Black), rc);
}
}
}