Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

178 Part II Programming Fundamentals



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


If lstCountryBox.SelectedIndex < 0 Then
lblGreeting.Text = "Please click the country name"
End If
This If statement evaluates the SelectedIndex property of the list box object by using
a conditional statement. The event handler assumes that if the SelectedIndex property
is zero or greater, the user doesn’t need help picking the country name (because he or
she has already selected a country). But if the SelectedIndex property is less than zero,
the event handler displays the message “Please click the country name” in the greeting
label at the bottom of the form. This Help message appears when the user holds the
pointer over the list box and disappears when a country name is selected.


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

  2. 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:


  1. Click a country name in the list box.


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


  1. 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.
Free download pdf