Bagikan melalui


Closer Look: Sharing an Event Handler

In this lesson, you will learn how to create a shared event handler that handles events for more than one control.

In the previous lesson, Making Your Program React to the User: Creating an Event Handler, you learned how to write code in response to the MouseEnter and MouseLeave events for a Button control. What happens, though, if you have two or more Button controls and you want to display the same message for all of them? You could write code in the event handlers for each control, but fortunately there is an easier way.

If you look closely at the event handler methods for the MouseEnter event, you will notice that the Method declaration (Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter) contains a Handles clause (Handles Button1.MouseEnter). Not surprisingly, the Handles keyword tells the event handler which events it should handle.

To share an event handler between multiple controls, you simply need to add the additional control names and the name of the event you want to handle. The event handler is then notified when the event occurs for any one of those controls. For example, if you had two Button controls and you wanted to use the same event handler for both, the Handles clause would look like the following.

Handles Button1.MouseEnter, Button2.MouseEnter.

You now have a single method that handles the MouseEnter event for both controls, but how does the event handler knows which control raised the event? If you look again at the Method declaration, you will notice the clause ByVal sender As Object—the Sender keyword tells the event handler which object (in this case which control) raised the event.

Try It!

To share an event handler

  1. Open the EventHandler project that you created in the previous lesson. If you didn't save it, you will first need to go back to the previous lesson, Making Your Program React to the User: Creating an Event Handler, and complete the procedures in that lesson.

  2. In Solution Explorer, select Form1.vb, and then on the View menu choose Designer.

  3. From the Toolbox, drag another Button control onto the form.

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

  5. On the View menu, choose Code to open the Code Editor.

  6. In the Button1_MouseEnter method declaration (Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter), change the Handles clause to read Handles Button1.MouseEnter, Button2.MouseEnter.

  7. In the body of the event declaration, replace the code with the following.

    If sender.Equals(Button1) Then
      Button1.Text = "The mouse has entered Button1" 
    Else
      Button2.Text = "The mouse has entered Button2" 
    End If
    

    This code checks to see if the sender was Button1—if so, then Button1's Text property is updated; if not, then Button2's Text property is updated.

  8. In the Button1_MouseLeave method declaration, change the Handles clause to read as follows.

    Handles Button1.MouseLeave, Button2.MouseLeave.

  9. In the body of the event declaration, replace the code with the following.

    sender.Text = "The mouse has left"
    

    In this case, the code sets the Text property of the sender (either Button1 or Button2) to the same string.

  10. Press F5 to run your application.

    Now when the mouse pointer passes over the button, the text changes to The mouse has entered, along with the name of the button, and when the mouse is no longer over the button, the text changes back to The mouse has left.

    Try adding more controls to the form and modifying the Handles clauses to include them—they don't even have to be the same kind of controls!

Next Steps

In this lesson, you learned how to share a single event handler between multiple controls. In the next lesson, you will learn how to use two new kinds of controls, the CheckBox and RadioButton controls, to present choices to users.

Next Lesson: Getting User Choices: Using Check Boxes and Radio Buttons

See Also

Tasks

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