Home
Map
Button ExampleCreate a Button control in Windows Forms and handle clicks with an event handler.
WinForms
This page was last reviewed on Oct 6, 2022.
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.
Shows a buttonShows a buttonShows a button
Add. In Visual Studio, go to the View menu and select the Toolbox option. Locate the Button item in the Toolbox window and either double-click it or drag it to your window.
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 type in button1_Click.
Detail 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.
MessageBox.Show
Shows a button
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?"); } } }
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 this way.Shows a button
Click. Windows Forms programs are event-based. When you add a Click event handler to a Button, the event handler method will be invoked when the user clicks on the button.Shows a button
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. The C# code will then be inserted.
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.
Discussion. By specifying the constraints of your Button, you can avoid modifying pixel dimensions. You should experiment with the Anchor property to control the resizing of the Button.
Further I recommend putting Buttons inside TableLayoutPanel cells, as this reduces the need to position them manually.
Enabled. Consider setting the Enable property's value to an expression based on another part of the user interface. This will keep the program UI in sync.
Summary. We explored the Button control in the Windows Forms control set. We added add a new instance of the Button control. We next changed the appearance of the Button control and its text.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 6, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.