Step 3: End the Game

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

To make the game end, you make the Finish label work. You do that by adding an event handler for the label's MouseEnter event.

Note Note

If you completed Tutorial 1: Create a Picture Viewer, you know about event handlers. Most controls have many different events that they can raise. The Picture Viewer used the Button control Click event and the CheckBox control CheckChanged event. In this tutorial, you use the Label control MouseEnter event, which gets raised every time the mouse pointer enters the control. The Label control has over four dozen events. Most have intuitive names, like DoubleClick, Resize, and TextChanged. A list of event names appears later in this tutorial.

link to videoFor a video version of this topic, see Tutorial 2: Create a Maze in Visual Basic - Video 2 or Tutorial 2: Create a Maze in C# - Video 2.

To end the game

  1. Select the finishLabel control, and then click the Event icon at the top of the Properties window, which is shaped like a lightning bolt. When you click it, instead of showing the control's properties, it shows the control's events. You can return to the list of properties by clicking the Property icon. For now, keep the Properties window as is, so it's showing all of the events for the finishLabel control. Scroll down to the MouseEnter event. The icons and the MouseEnter event appear as follows.

    Event icon

    Event icon
    Property icon

    Property icon
    MouseEnter event

    MouseEnter event
  2. Double-click the word MouseEnter. After you do, the IDE automatically adds an event handler method to your form and shows it to you in the code editor, as follows.

    Private Sub finishLabel_MouseEnter(sender As System.Object, e As System.EventArgs) Handles finishLabel.MouseEnter
    
    End Sub
    

    This event handler method runs every time the mouse pointer enters the label.

  3. You want the program to open a message box that shows "Congratulations," and then you want the program to close. To do that, add lines of code (with a comment), as follows.

    Private Sub finishLabel_MouseEnter(sender As System.Object, e As System.EventArgs) Handles finishLabel.MouseEnter
        ' Show a congratulatory MessageBox, then close the form.
        MessageBox.Show("Congratulations!")
        Close()
    End Sub
    
    Note Note

    Your finishLabel_MouseEnter() method has two statements. The first statement is calling a method called Show(), which opens a message box that contains whatever text you put inside the parentheses.

  4. You can learn more about what's happening by using the IDE to explore your code. Take your mouse pointer and position it so it's over the word MessageBox. You should see the following tooltip.

    Tooltip

    Tooltip
    Note Note

    The IDE shows that there's a class called System.Windows.Forms.MessageBox, and the Show() method that you called is inside that class. You don't need a complete understanding to make the message box work, but additional information can be helpful.

    Regarding the second statement, every form has a built-in method called Close() that causes the form to close. Some programs have several windows that the user can switch between. When working on a program like that, it closes the current window, but leaves the rest of the program running. (For example, if you have several Microsoft Office Word documents open at the same time, closing one document window closes that document, but Office Word stays open.) However, in a program like this one, where there's only one window, closing that window causes the program to stop running, so closing the form closes your program.

  5. Save and run your program. Move your mouse pointer over the Finish label. It should open the message, and then close the program.

To continue or review

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft