Button

Executes an operation, when it is clicked.
API: System.Windows.Forms.Button
Naming Convension: btnxxxxxx
button
Properties of Button

Property Description
Name Specifies the name of the control.
Text Specifies the displayable text of the control.
BackColor Specifies the background color of the control.
ForeColor Specifies the foreground color of the control.
Font Specifies the font style of the control’s text.
Enabled Enables / Disables the control.
Visible Displays / Hides the control.
Cursor Specifies the mouse pointer style, when it is over on the control.
Size Specifies the Width and Height of the control.
Location Specifies the X and Y co-ordinations of the control’s position on the form.
TextAlign Specifies the position of the text in the control.
Image Specifies the image that is to be displayed in the control along with the text.
ImageAlign Specifies the position of the image in the control
TabIndex Specifies the index of the control in the tab order.
ContextMenuStrip Contains the reference of the respective context menu control.

Events of Button
Event Description
Click Executes when the user clicks the control run time.
DoubleClick Executes when the user double-clicks the control at run time.
MouseMove Executes when the mouse pointer is moves across the control.
MouseEnter Executes when the mouse pointer is focused on to the control.
MouseLeave Executes when the mouse pointer is out of the control.
KeyPress Executes when any key is pressed on the keyboard, while the focus is on the control.
Enter Executes when the focus is got on to the control.
Leave Executes when the focus is out of the control.

Demo on Buttons

Design
button1:
Text: Red
Name: btnRed

button2:
Text: Green
Name: btnGreen

button3:
Text: Blue
Name: btnBlue
button

private void btnRed_Click(object sender, EventArgs e)
{
this.BackColor = Color.Red;
}

private void btnGreen_Click(object sender, EventArgs e)
{
this.BackColor = Color.Green;
}

private void btnBlue_Click(object sender, EventArgs e)
{
this.BackColor = Color.Blue;
}

Demo on Buttons

Design
button1:
Text: Normal
Name: btnNormal

button2:
Text: Minimize
Name: btnMinimize

button3:
Text: Maximize
Name: btnMaximize

button4:
Text: Exit
Name: btnExit
button

private void btnNormal_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}

private void btnMinimize_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}

private void btnMaximize_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}

private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}

Demo on Buttons

Design
button1:
Text: Show Background Image
Name: btnShowBackgroundImage

button2:
Text: Clear Background Image
Name: btnClearBackgroundImage
button

private void btnShowBackgroundImage_Click(object sender, EventArgs e)
{
this.BackgroundImage = Image.FromFile("c:\\globe.jpg");
this.BackgroundImageLayout = ImageLayout.Zoom;
}
private void btnClearBackgroundImage_Click(object sender, EventArgs e)
{
this.BackgroundImage = null;
}