One Step Further: Detecting Mouse Events


One Step Further: Detecting Mouse Events

I began this chapter by discussing a few of the events that Visual Basic programs can respond to, and as the chapter progressed, you learned how to manage different types of events by using the If…Then and Select Case decision structures. In this section, you'll add an event handler to the Select Case program that detects when the pointer “hovers” over the Country list box for a moment or two. You'll write the special routine, or event handler, by building a list box event procedure for the MouseHover event, one of several mouse-related activities that Visual Basic can monitor and process. This event procedure will display the message “Please click the country name” if the user holds the mouse over the country list box for a moment or two but doesn't make a selection, perhaps because he or she doesn't know how to make a selection or has become engrossed in another task.

Add a mouse event handler

  1. Open the Code Editor if it isn't already open.

  2. At the top of the Code Editor, click the Class Name arrow, and then click the lstCountryBox object.

    You can use the ScreenTip feature to help identify elements like the Class Name list box in Visual Studio, which is another example of the MouseHover event within the IDE.

  3. Click the Method Name arrow, and then click the MouseHover event.

    Visual Basic opens the lstCountryBox_MouseHover event procedure in the Code Editor, as shown here:

    graphic

    Each object on the form has one event procedure that opens automatically when you double-click the object on the form. You need to open the remaining event procedures by using the Method Name list box.

  4. Type the following program statements in the lstCountryBox_MouseHover event procedure:

    If lstCountryBox.SelectedIndex < 0 Or _    lstCountryBox.SelectedIndex > 4 Then      lblGreeting.Text = "Please click the country name"  End If

    This If statement evaluates the SelectedIndex property of the list box object by using two conditional statements and the Or operator. The event handler assumes that if there's a value between 1 and 4 in the SelectedIndex property, the user doesn't need help picking the country name (he or she has already selected a country). But if the SelectedIndex property is outside that range, the event handler displays the message “Please click the country name” in the greeting label at the bottom of the form. This message appears when the user holds the pointer over the list box and disappears when a country name is selected.

  5. Click the Start Debugging button to run the program.

  6. Hold the pointer over the country list box, and wait a few moments.

    The message “Please click the country name” appears in red text in the label, as shown here:

    graphic

  7. Click a country name in the list box.

    The translated greeting appears in the label, and the help message disappears.

  8. Click the Quit button to stop the program.

You've learned how to process mouse events in a program, and you've also learned that writing event handlers is quite simple. Try writing additional event handlers on your own as you continue reading this book—it will help you learn more about the events available to Visual Studio objects, and it will give you more practice with If…Then and Select Case decision structures.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net