Step 3: End the Game
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.
For 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.
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.
To end the game
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.
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.
This event handler method runs every time the mouse pointer enters the label.
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.
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.
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.
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.
Save and run your program. Move your mouse pointer over the Finish label. It should open the message, and then close the program.