Export (0) Print
Expand All

Creating Event Handlers on the Windows Forms Designer

Visual Studio .NET 2003

In nearly all applications, it is essential to respond to user or system events. In C# and C++, you can create event handlers using the Properties window. In Visual Basic, you can use the Class Name and Method Name drop-down boxes in the Code Editor. For more information on creating default-named event handlers, see Creating Default Event Handlers on the Windows Forms Designer.

To create an event handler in Visual Basic

  1. Right-click the form and choose View Code.
  2. From the Class Name drop-down box, select the control that you want to add a specific event handler to.
  3. From the Method Name drop-down box, select the event for which you want to add a specific handler.
  4. The Code Editor inserts the appropriate event handler and positions the insertion point within the method. In the example below, it is the Click event for the Button control.
    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       ' Add event handler code here.
    End Sub
    
  5. Add the appropriate code to the event handler.

To create an event handler in C# or C++

  1. Click the form or control that you want to create an event handler for.
  2. In the Properties window, click the Events button (Aa984320.vxeventsbutton_propertieswindow(en-us,VS.71).gif).
  3. In the list of available events, click the event that you want to create an event handler for.
  4. In the box to the right of the event name, type the name of the handler and press ENTER.
    Tip   Name the event handler according to the functionality of the event; for example, for the Click event, you can type StartProcess as the event handler.

    The Code Editor appears, showing the code for the form, and an event handler method is generated in your code similar to the following:

    // C#
    private void StartProcess(object sender, System.EventArgs e) 
    {
       // Add event handler code here.
    }
    // C++
    private:
      System::Void StartProcess(System::Object *  sender,
        System::EventArgs *  e)
      {
        // Add event handler code here.
      }
    
  5. Add the appropriate code to the event handler.

See Also

Event Handling in Windows Forms | Introduction to Event Handlers in Windows Forms | Creating Default Event Handlers on the Windows Forms Designer | Creating Windows Forms

Show:
© 2014 Microsoft