Button In C#

Button class in Windows Forms represents a C# Button control. This article demonstrates how to use a Button and its propertoes and methods in a Windows Forms application using C#.

C# Button class in .NET Framework class library represents a Windows Forms Button control. A Button control is a child control placed on a Form and used to process click event and can be clicked by a mouse click or by pressing ENTER or ESC keys.

 

Creating a C# Button

 
To create a Button control, you simply drag and drop a Button control from Toolbox to Form in Visual Studio. After you drag and drop a Button to a Form, the Button looks like Figure 1. Once a Button is on the Form, you can move it around and resize it.
 
ButtonImg1.jpg
Figure 1
 

Setting Button Properties

 
After you place a Button control on a Form, the next step is to set button properties.
 
The easiest way to set a Button control properties is by using the Properties Window. You can open Properties window by pressing F4 or right click on a control and select Properties menu item. The Properties window looks like Figure 2.
 
ButtonImg2.jpg
Figure 2
 

Background and Foreground Color of a Button Control

 
BackColor and ForeColor properties are used to set background and foreground color of a Button respectively. If you click on these properties in Properties window, the Color Dialog pops up.
 
Alternatively, you can set background and foreground colors at run-time. The following code snippet sets BackColor and ForeColor properties.
  1. // Set background and foreground  
  2. dynamicButton.BackColor = Color.Red;  
  3. dynamicButton.ForeColor = Color.Blue;  

Auto Ellipsis in Button

 
An ellipsis character (...) is used to give an impression that a control has more characters but it could not fit in the current width of the control. Figure 3 shows an example of an ellipsis character.  

 

ButtonImg3.jpg
Figure 3

 

If AutoEllipsis property is true, it adds ellipsis character to a control if text in control does not fit. You may have to set AutoSize to false to see the ellipses character.
 

Image in Button

 
The Image property of a Button control is used to set a button background as an image. The Image property needs an Image object. The Image class has a static method called FromFile that takes an image file name with full path and creates an Image object.
 
You can also align image and text. The ImageAlign and TextAlign properties of Button are used for this purpose.
 
The following code snippet sets an image as a button background.
  1. // Assign an image to the button.  
  2. dynamicButton.Image = Image.FromFile(@"C:\Images\Dock.jpg");  
  3. // Align the image and text on the button.  
  4. dynamicButton.ImageAlign = ContentAlignment.MiddleRight;  
  5. dynamicButton.TextAlign = ContentAlignment.MiddleLeft;  
  6. // Give the button a flat appearance.  
  7. dynamicButton.FlatStyle = FlatStyle.Flat;  

Text and Font

 
The Text property of Button represents the contents of a Button. The TextAlign property if used to align text within a Button that is of type ContentAlignment enumeration.
 
The Font property is used to set font of a Button.
 
The following code snippet sets Text and Font properties of a Button control.
  1. dynamicButton.Text = "I am Dynamic Button";  
  2. dynamicButton.TextAlign = ContentAlignment.MiddleLeft;  
  3. dynamicButton.Font = new Font("Georgia", 16);  

Button Control States

 
Button control has five states - Normal, Flat, Inactive, Pushed, and All. ButtonState enumeration represents a button state.
 
Unfortunately, Windows Forms does not have a straight-forward way to set a button control state but there is a work around.
 
Windows Forms has a ControlPaint class with some static methods that can be used to draw various controls at runtime. The DrawButton method is used to draw a Button control and the last parameter of this method is ButtonState enumeration.
 
The following code snippet sets the button state of button1 using ControlPaint class.
  1. ControlPaint.DrawButton(System.Drawing.Graphics.FromHwnd(button1.Handle), 0, 0, button1.Width, button1.Height, ButtonState.Pushed);  

Adding Button Click Event Hander

 
A Button control is used to process the button click event. We can attach a button click event handler at run-time by setting its Click event to an EventHandler obect. The EventHandler takes a parameter of an event handler. The Click event is attached in the following code snippet.
  1. // Add a Button Click Event handler  
  2. dynamicButton.Click += new EventHandler(DynamicButton_Click);  
The signature of Button click event handler is listed in the following code snippet.
  1. private void DynamicButton_Click(object sender, EventArgs e)  
  2. { }  

Creating a C# Button Dynamically

 
Creating a Button control at run-time is merely a work of creating an instance of Button class, set its properties and add Button class to the Form controls.
 
First step to create a dynamic button is to create an instance of Button class. The following code snippet creates a Button control object.
  1. // Create a Button object  
  2. Button dynamicButton = new Button();  
Next step, you need to set Button class properties. You need to make sure to specify the Location, Width, Height or Size properties. The default location of Button is left top corner of the Form. The Location property takes a Point that specifies the starting position of the Button on a Form. The Size property specifies the size of the control. We can also use Width and Height property instead of Size property. The following code snippet sets Location, Width, and Height properties of a Button control.
  1. // Set Button properties  
  2. dynamicButton.Location = new Point(20, 150);  
  3. dynamicButton.Height = 40;  
  4. dynamicButton.Width = 300;  
In the next step, you may set more properties of the Button control.
 
The following code snippet sets background color, foreground color, Text, Name, and Font properties of a Button.
  1. // Set background and foreground  
  2. dynamicButton.BackColor = Color.Red;  
  3. dynamicButton.ForeColor = Color.Blue;  
  4. dynamicButton.Text = "I am Dynamic Button";  
  5. dynamicButton.Name = "DynamicButton";  
  6. dynamicButton.Font = new Font("Georgia", 16);  
A Button control is used to process the button click event. We can attach a button click event handler at run-time by setting its Click event to an EventHandler obect. The EventHandler takes a parameter of an event handler. The Click event is attached in the following code snippet.
  1. // Add a Button Click Event handler  
  2. dynamicButton.Click += new EventHandler(DynamicButton_Click);  
The signature of Button click event handler is listed in the following code snippet.
  1. private void DynamicButton_Click(object sender, EventArgs e)  
  2. { }  
Now the last step is adding a Button control to the Form. The Form.Controls.Add method is used to add a control to a Form. The following code snippet adds a Button control to the current Form.
  1. Controls.Add(dynamicButton);  
The complete code is listed in the following code, where CreateDynamicButton methods creates a Button control to a Form at run-time, attaches a click event handler of the button and adds Button control to the Form by calling Form.Controls.Add() method.
  1. /// <summary>  
  2. /// This method creates a Button control at runtime  
  3. /// </summary>  
  4.   
  5. private void CreateDynamicButton()  
  6. {  
  7.   
  8.    // Create a Button object  
  9.    Button dynamicButton = new Button();  
  10.   
  11.    // Set Button properties  
  12.    dynamicButton.Height = 40;  
  13.    dynamicButton.Width = 300;  
  14.    dynamicButton.BackColor = Color.Red;  
  15.    dynamicButton.ForeColor = Color.Blue;  
  16.    dynamicButton.Location = new Point(20, 150);  
  17.    dynamicButton.Text = "I am Dynamic Button";  
  18.    dynamicButton.Name = "DynamicButton";  
  19.    dynamicButton.Font = new Font("Georgia", 16);  
  20.   
  21.    // Add a Button Click Event handler  
  22.    dynamicButton.Click += new EventHandler(DynamicButton_Click);  
  23.   
  24.    // Add Button to the Form. Placement of the Button  
  25.    // will be based on the Location and Size of button  
  26.    Controls.Add(dynamicButton);  
  27. }  
  28. /// <summary>  
  29. /// Button click event handler  
  30. /// </summary>  
  31.   
  32. /// <param name="sender"></param>  
  33. /// <param name="e"></param>  
  34.   
  35. private void DynamicButton_Click(object sender, EventArgs e)  
  36. {  
  37.    MessageBox.Show("Dynamic button is clicked");  
  38. }  
You need to make sure to call CreateDynamicButton() method on the Form's constructor just after InitializeComponent() method, listed as following.
  1. public Form1()  
  2. {  
  3.    InitializeComponent();  
  4.    CreateDynamicButton();  
  5. }  

Summary

 
In this article, we discussed how to create Button control in Windows Forms using C# at design-time as well as at run-time. We also saw how to set a button properties and set the button click event handler.