How to: Consume Events in a Windows Forms Application
A common scenario in Windows Forms applications is to display a form with controls, and then perform a specific action based on which control the user clicks. For example, a Button control raises an event when the user clicks it in the form. By handling the event, your application can perform the appropriate application logic for that button click.
For more information about Windows Forms, see Getting Started with Windows Forms.
To handle a button click event on a Windows Form
Create a Windows Form that has a Button control.
Private WithEvents myButton As Button
private Button button;
private: Button^ button;
Define an event handler that matches the Click event delegate signature. The Click event uses the EventHandler class for the delegate type and the EventArgs class for the event data.
Private Sub Button_Click(sender As Object, e As EventArgs) '... End Sub
private void Button_Click(object sender, EventArgs e) { //... }
private: void Button_Click(Object^ sender, EventArgs^ e) { //... }
Add the event handler method to the Click event of the Button.
AddHandler myButton.Click, AddressOf Me.Button_Click
button.Click += new EventHandler(this.Button_Click);
button->Click += gcnew EventHandler(this, &SnippetForm::Button_Click);
Note
A designer (such as Visual Studio 2005) will do this event wiring for you by generating code that is similar to the code in this example.
Example
The following code example handles the Click event of a Button to change the background color of a TextBox. The elements in bold show the event handler and how it is wired to the Click event of the Button.
The code in this example was written without using a visual designer (such as Visual Studio 2005) and contains only essential programming elements. If you use a designer, it will generate additional code.
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Public Class MyForm
Inherits Form
Private box As TextBox
Private WithEvents myButton As Button
Public Sub New()
box = New TextBox()
box.BackColor = System.Drawing.Color.Cyan
box.Size = New Size(100, 100)
box.Location = New Point(50, 50)
box.Text = "Hello"
myButton = New Button()
myButton.Location = New Point(50, 100)
myButton.Text = "Click Me"
AddHandler myButton.Click, AddressOf Me.Button_Click
Controls.Add(box)
Controls.Add(myButton)
End Sub
' The event handler.
Private Sub Button_Click(sender As Object, e As EventArgs)
box.BackColor = System.Drawing.Color.Green
End Sub
' The STAThreadAttribute indicates that Windows Forms uses the
' single-threaded apartment model.
<STAThread> _
Public Shared Sub Main()
Application.Run(New MyForm())
End Sub
End Class
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
public class MyForm : Form
{
private TextBox box;
private Button button;
public MyForm() : base()
{
box = new TextBox();
box.BackColor = System.Drawing.Color.Cyan;
box.Size = new Size(100,100);
box.Location = new Point(50,50);
box.Text = "Hello";
button = new Button();
button.Location = new Point(50,100);
button.Text = "Click Me";
// To wire the event, create
// a delegate instance and add it to the Click event.
button.Click += new EventHandler(this.Button_Click);
Controls.Add(box);
Controls.Add(button);
}
// The event handler.
private void Button_Click(object sender, EventArgs e)
{
box.BackColor = System.Drawing.Color.Green;
}
// The STAThreadAttribute indicates that Windows Forms uses the
// single-threaded apartment model.
[STAThread]
public static void Main()
{
Application.Run(new MyForm());
}
}
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
public ref class MyForm : Form
{
private:
TextBox^ box;
Button^ button;
public:
MyForm() : Form()
{
box = gcnew TextBox();
box->BackColor = System::Drawing::Color::Cyan;
box->Size = System::Drawing::Size(100,100);
box->Location = System::Drawing::Point(50,50);
box->Text = "Hello";
button = gcnew Button();
button->Location = System::Drawing::Point(50,100);
button->Text = "Click Me";
// To wire the event, create
// a delegate instance and add it to the Click event.
button->Click += gcnew EventHandler(this, &MyForm::Button_Click);
Controls->Add(box);
Controls->Add(button);
}
private:
// The event handler.
void Button_Click(Object^ sender, EventArgs^ e)
{
box->BackColor = System::Drawing::Color::Green;
}
// The STAThreadAttribute indicates that Windows Forms uses the
// single-threaded apartment model.
public:
[STAThread]
static void Main()
{
Application::Run(gcnew MyForm());
}
};
int main()
{
MyForm::Main();
}
Compiling the Code
Save the preceding code to a file (with a .cs extension for a C# file and .vb for Visual Basic 2005), compile, and execute. For example, if the source file is named WinEvents.cs (or WinEvents.vb), run the following command:
vbc /r:System.DLL /r:System.Windows.Forms.dll /r:System.Drawing.dll WinEvents.vb
csc /r:System.DLL /r:System.Windows.Forms.dll /r:System.Drawing.dll WinEvents.vb
cl /clr:pure WinEvents.cpp
Your executable file will be named WinEvents.exe.