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

    
    private void Button_Click(object sender, EventArgs e)
    {
        //...
    }
    
    
    
  3. Add the event handler method to the Click event of the Button.

    
    button.Click += new EventHandler(this.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.

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.


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());
    }
}


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


Your executable file will be named WinEvents.exe.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft