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

  1. Create a Windows Form that has a Button control.

    private Button button;
    
    Private WithEvents myButton As Button
    
  2. 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.

    void Button_Click(object sender, EventArgs e)
    {...}
    
    Sub Button_Click(sender As Object, e As EventArgs)
    ...
    End Sub
    
  3. Add the event handler method to the Click event of the Button.

    button.Click += new EventHandler(this.Button_Click);
    
    AddHandler myButton.Click, AddressOf Me.Button_Click
    
    NoteNote

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

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.
   [STAThreadAttribute]
   public static void Main(string[] args) 
   {
      Application.Run(new MyForm());
   }
}
Option Explicit
Option Strict

Imports System
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.
   <STAThreadAttribute()> _
   Public Shared Sub Main(args() As String)
      Application.Run(New MyForm())
   End Sub
End Class

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:

csc /r:System.DLL /r:System.Windows.Forms.DLL /r:System.Drawing.DLL WinEvents.cs
vbc /r:System.DLL /r:System.Windows.Forms.DLL /r:System.Drawing.DLL WinEvents.vb

Your executable file will be named WinEvents.exe.

See Also

Concepts

Events and Delegates
Consuming Events
Raising an Event

Other Resources

Handling and Raising Events