Making Your Program React to the User: Creating an Event Handler

In this lesson, you will learn how to create an event handler.

As you have seen in previous lessons, controls have properties, methods, and events and are used to create the user interface. Events are interesting things that can happen to a control. For example, a control can be clicked, text can be typed into it, the mouse pointer can move across it, and so forth.

When something interesting occurs, the control raises an event. This means that it sends a signal to the program to let it know that something has occurred. The program then checks if it has any methods to handle that event. Such methods are called event handlers. An example is a method that runs when a button is clicked, such as the method that you created in Interacting with the User: Using Buttons.

Controls can raise various kinds of events, but there is always one default event for each control. You can create event handlers for a variety of control events. In this lesson, you will create event handlers to handle a button's default event handler, the Click event. This is the event that occurs when you click the button. Next, you'll create event handlers to handle the button's MouseEnter and MouseLeave events. These are the events that occur when a mouse moves across a control.

Try It!

To handle the Click event

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type EventHandler, and click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag a Button control onto the form.

  5. In the Properties window, set the AutoSize property to True.

  6. On the View menu, click Code to open the Code Editor.

    Just above the Code Editor, note the two drop-down boxes. The box on the left contains a list of all the controls on the form, in addition to Form1, (General), and (Form1 Events). The box on the right lists each event available for the item listed in the box on the left.

  7. In the box on the left, click Button1.

  8. In the box on the right, click Click.

    A new event handler called Button1_Click appears in the Code Editor.

    Note

    You can enter the default event handler for a control by double-clicking the control on the form.

  9. In the Button1_Click event handler, type the following code.

    MsgBox("You clicked the button")
    
  10. Press F5 to run your application. When you click Button1, a message box appears.

Adding Other Event Handlers

You can write code in additional event handlers for the same control. For example, you can change the text that appears on the button when a user moves the mouse pointer over the button.

To handle the MouseEnter event

  1. In the Code Editor, make sure that Button1 is selected in the drop-down box on the left, and then click MouseEnter in the drop-down box on the right.

    A new event handler called Button1_MouseEnter appears in the Code Editor.

  2. In the Button1_MouseEnter event handler, type the following code.

    Button1.Text = "The Mouse has entered"
    

    Press F5 to run your application. Pass the mouse pointer over the button. Note that when the mouse pointer passes over Button1, the text on the button changes.

You might have noticed in the previous example that though the text of Button1 changes when the mouse pointer passes over it, the text does not change back when the mouse pointer leaves. If you want the text to change when the mouse is no longer over the button, you must handle the MouseLeave event and also the MouseEnter event.

To handle the MouseLeave event

  1. In the Code Editor, make sure that Button1 is selected in the drop-down box on the left, and then click MouseLeave in the drop-down box on the right.

    A new event handler called Button1_MouseLeave appears in the Code Editor.

  2. In the Button1_MouseLeave event handler, type the following code.

    Button1.Text = "The mouse has left"
    
  3. Press F5 to run your application.

    Now when the mouse pointer passes over the button, the text changes to The mouse has entered, but when the mouse is no longer over the button, the text changes to The mouse has left.

Next Steps

In this lesson, you learned how to create an event handler by using the Code Editor. At this point, you can either go to the next lesson in the sequence, Getting User Choices: Using Check Boxes and Radio Buttons or examine event handlers with Closer Look: Sharing an Event Handler. If you choose the second option, you should save your EventHandler project for use in that lesson.

See Also

Reference

Label Control Overview (Windows Forms)

TextBox Control Overview (Windows Forms)

Other Resources

Creating the Visual Look of Your Program: Introduction to Windows Forms

Control Property, Method, and Event Changes for Visual Basic 6.0 Users