C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Action: You can use a Button control to perform an action when the user clicks or presses a key to activate the button.
EventAnd: Now, when you see the Button on the window that was inserted, you can right-click on it and select Properties.
Next: We see how the Button properties were changed so that the Button contains the text "Open" instead of the "button1" default label.
Note: Windows Forms provides some automatic features that will turn an Enter keypress into a Click event in some cases.
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.
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.
MessageBoxWindows 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("The Dev Codes says hello.",
"How is your day going?");
}
}
}
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.
TableLayoutPanelAlso: You can assign multiple Enabled properties on controls to each other to ensure the user interface is usable at all times.