A button accepts clicks. In Windows Forms we use a Button control that accepts click events and performs other actions in the user interface. This control provides a way to accept input—and invoke logic based on that input.

This C# tutorial shows how to use the Button control from Windows Forms.

First, this tutorial uses the Windows Forms control set. The Button control is one control you can add to any Windows Forms program. It renders as a rectangular box with text or images in the center that you can resize and add event handlers to.
Perform an action. So you can use a Button control to perform an action when the user clicks or presses a key to activate the button. You can create a new Windows Forms project by opening Visual Studio or Visual Studio Express and go to File and New Project, and then Visual C#, Windows Forms Application.
Let's describe how you can add a Button control to a Windows Forms program. In Visual Studio, make sure the Window you want to add the button to is shown. Go to the View menu and select the Toolbox option. This presents a list of all the Windows Forms controls you can add to your project on the screen. Locate the Button item in the Toolbox window and either double-click it or drag it to your window. Now, when you see the Button on the window that was inserted, you can right-click on it and select Properties.
Here we explore the Button properties that you can change from within the Visual Studio designer. In the properties pane or window, you can either change the regular properties or the event handlers on the Button control. If you want to change how the button appears on the screen, or its text contents, you can alter properties through the properties window. In the following screenshot, we see how the Button properties were changed so that the Button contains the text "Open..." instead of the "button1" default label.

Using ellipsis (three periods). In this article, we use three periods after the Text property on the Button control in the properties window. The reason we use three periods (an ellipsis) is that the Button in our example will be used to open a dialog window. In user interface design, the ellipsis is used to signal that an action that requires the user's attention such as a dialog box will occur if the control is activated as by clicking on it. I feel that using ellipses (...) correctly on strings in programs is one of the most important user interface correctness issues.
Here we actually add an event handler on the Button control in the tutorial. Windows Forms programs are event-based, which means they are idle until the user takes an action upon controls in the user interface. When you add a Click event handler to a Button, the event handler method will be invoked when the user clicks on the button. Windows Forms provides some automatic features that will turn an Enter keypress into a Click event in some cases.


Using lightning bolt. In the Visual Studio properties window, there exists a lightning bolt icon and this is signifies the event handlers list. You can click on the lightning bolt icon and then double-click on an event. At that point, the C# code (or VB.NET code) will be inserted into the user-code portion of the project. You then manually edit that C# code to add the program-specific logic.
Wiring multiple events to one method. Instead of double-clicking to create a new event handler for one control event, you can use the drop-down box to select a method that already exists to handle the events. There are some restrictions on this: for example, the method signature must be compatible. However, wiring multiple events to one event handler is very useful and can simplify the source code of medium complexity programs greatly.
Windows Forms code that uses Button control [C#]
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication21
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Dot Net Perls says hello.", "How is your day going?");
}
}
}Using MessageBox.Show when Button is clicked. The above code text shows the body of the Click event that can be added to handle click events for the button1 control instance. You do not need to manually type in the "button1_Click" method signature, and doing so is much harder to maintain. Instead, use the interface in Visual Studio to create empty event handler methods. Then, you can insert logic such as calling the MessageBox.Show method call to perform a trivial action when the event is processed.
MessageBox.Show Examples
Let's consider various tips and tricks you can use to enhance the Button functionality in your Windows Forms controls. Generally, by specifying the constraints of your Button, you can avoid modifying pixel dimensions for the most part. You should experiment with the Anchor property to control the resizing of the Button if it is in a variable-sized window. Also, I recommend putting Buttons inside TableLayoutPanel cells, as this reduces the need to position them manually.
TableLayoutPanel in Windows FormsButton Enabled property. You can at any time change the enabled state of buttons by setting "button1.Enabled = true" or "button1.Enabled = false", for example. I recommend tying the Enable property's value to an expression based on another part of the user interface. You can similarly assign multiple Enabled properties on controls to each other to ensure the user interface is coherent and usable at all times.

We explored the Button control in the Windows Forms control set: we saw how you can create a new Windows Forms project, add a new instance of the Button control, change the appearance of the Button control and its text, and add an event to the Button control.
Windows Forms