C# Button Example

Create a Button control in Windows Forms and handle clicks with an event handler.

Button. 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.

Start. 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.
Action: You can use a Button control to perform an action when the user clicks or presses a key to activate the button.

Add. 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.
And: Now, when you see the Button on the window that was inserted, you can right-click on it and select Properties.

Text. In the properties pane or window, you can either change the regular properties or the event handlers on the Button control. You can alter any Button property through the properties window.
Next: 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). Here we use three periods after the Text property on the Button control in the properties window. We use three periods (an ellipsis) because the Button in our example will be used to open a dialog window.

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 important.

Click. Windows Forms programs are event-based. They are idle until the user takes an action upon a control. When you add a Click event handler to a Button, the event handler method will be invoked when the user clicks on the button.
Note: Windows Forms provides some automatic features that will turn an Enter keypress into a Click event in some cases.

Properties window. In this window, a lightning bolt signifies the event handlers list. You can click on it and then double-click on an event. At that point, the C# code will be inserted into the user-code portion of the project.
Then: You manually edit that C# code to add the program-specific logic. Any statements can be used.
And: Instead creating a new event handler, you can use the drop-down box to select a method that already exists to handle the events.
Info: The method signature must be compatible. Wiring multiple events to one event handler is useful.

Example. We see 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.
Instead: You can 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.
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?"); } } }

Discussion. 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.
Tip: You should experiment with the Anchor property to control the resizing of the Button if it is in a variable-sized window.
Further: I recommend putting Buttons inside TableLayoutPanel cells, as this reduces the need to position them manually.

Button Enabled property. You can at any time change the enabled state of buttons by setting Enabled to true or false. I recommend tying the Enable property's value to an expression based on another part of the user interface.
Also: You can assign multiple Enabled properties on controls to each other to ensure the user interface is usable at all times.

Summary. We explored the Button control in the Windows Forms control set. First we created a new Windows Forms project. We added add a new instance of the Button control. We next changed the appearance of the Button control and its text.Controls

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.