Button Control.
The Button control can be used to initiate a function, confirm an entry, compare
values and many other actions. The button can be clicked by the use of the mouse
or if focused with the enter key or the space bar. The button can be disabled and
only enabled under certain application conditions or changes.

Namespace: System.Windows.Forms
Assembly: Systems.Windows.Forms.dll
using System.Windows.Forms;
Calling: System.Windows.Forms.Button
Creating a new System.Windows.Forms.Button control.
Button button1 = new Button();
Adding the button control to a Windows form.
this.Controls.Add(button1);
Placing the button on the form at a specific location.
button1.Location = new Point(50, 50);
Assigning a text value to a button control.
button1.Text = "Enter";
Assigning a click event to the button control.
button1.Click+=new EventHandler(button1_Click);
Responding to the buttons click event.
private void button1_Click(object sender, EventArgs e)
{
//The MessageBox is part of the System.Windows.Forms namespace.
MessageBox.Show("HelloWorld");
}
Changing the background color of the button control.
button1.BackColor = System.Drawing.Color.Red;
To disable the button control.
button1.Enabled = false;
Here the Button control is still visible (partially faded) but will not respond
to any events. To re-enable the Button control you would set the Enabled property
to true. When adding the button control the default setting is for the control to
be Enabled.
Adding an image to the Button control.
try
{
button1.Image = Image.FromFile(@"c:\Icons\helloworld.gif");
//Set the Image relationship.
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
}
catch (System.IO.FileNotFoundException ex)
{
MessageBox.Show(ex.ToString());
}
The image can either be from a file or streamed via a datastream. Remember to ensure
that the image exists.
To align the image for the button control.
button1.ImageAlign = ContentAlignment.MiddleLeft;
|