Step 5: Add a MouseEnter Event Handler for Each Wall

The maze game would be more fun and challenging if it sends the user's mouse pointer back to the start every time the pointer touches a wall. Before you read further, consider how to do that.

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

To add a MouseEnter event handler for each wall

  1. Go to Windows Forms Designer and click any of your newly added walls.

  2. Go to the Properties window and click the Event icon to display the events for that wall. Scroll down to the MouseEnter event. Instead of double-clicking it, type the text wall_MouseEnter, and then press ENTER. The Event icon and Properties window appear as follows.

    Event icon

    Event icon

    Properties window showing MouseEnter event

    Properties window showing MouseEnter event

    Note

    When you type the event name directly into the event table in the Properties window, you direct the IDE to create an event handler with that name and connect it to the control's event. Often, you want the IDE to choose event names, because the names are logical, and using names makes it easier for others to read and understand your code. When the IDE chooses a name for an event handler, it uses the name of the control and the name of the event. In this case, you didn't change the default names of your walls, which are label4, label18, label25, and so on. So if you click a wall named label12, the IDE would have named the event handler label12_MouseEnter. By typing the name wall_MouseEnter, you're choosing a more applicable name. This is especially important when you use one event handler for multiple controls, which is what you do later in this tutorial.

  3. After you press ENTER, the IDE adds a new event handler for you and connects it to that wall's MouseEnter event. The newly added code should appear in your code editor as follows. In Visual Basic, the specific label may not be Label8, as shown in the code.

    Private Sub wall_MouseEnter() Handles Label8.MouseEnter
    
    End Sub
    
    private void wall_MouseEnter(object sender, EventArgs e)
    {
    
    }
    
  4. Next, add a call to your MoveToStart() method, along with a comment explaining the method. Start by going to your method and adding the statement MoveToStart(). An IntelliSense window opens, and the following appears.

    IntelliSense window

    IntelliSense window

    When you added your MoveToStart() method, the IDE added it to the IntelliSense window. The XML comment that you added appears in the tooltip. This is useful when you write programs with numerous methods.

  5. Press TAB to direct IntelliSense to complete the method name. If you're writing Visual C# code, remember to add the semicolon (;) at the end of the statement. Then add a comment above the statement. Your code should look like the following. In Visual Basic, the specific label may not be Label8, as shown in the code.

    Private Sub wall_MouseEnter() Handles Label8.MouseEnter
        ' When the mouse pointer hits a wall or enters the panel, 
        ' call the MoveToStart() method.
        MoveToStart()
    End Sub
    
    private void wall_MouseEnter(object sender, EventArgs e)
    {
        // When the mouse pointer hits a wall or enters the panel, 
        // call the MoveToStart() method.
        MoveToStart();
    }
    
  6. Save and run your program. Move your mouse pointer over the wall that you connected the event handler to. (If you don't remember which one you chose, move your mouse pointer over each wall until you find the right one.) As soon as you touch it, it should send your mouse pointer back to the start.

    Next, you want to do the same for the rest of the walls. You could write the same MouseEnter event handler for each of the walls. But that process would be lengthy, would result in multiple lines of the same code in your program, and would be difficult to change. The IDE provides an easier way to connect the same event handler to all of the walls.

  7. Go to Windows Forms Designer, and from the Edit menu, click Select All.

  8. Hold down the CTRL key, and then click the Finish label to clear the selection. This should leave all of the walls and the panel selected.

  9. Now go to the event table on the Properties window. Scroll down to the MouseEnter event and click the edit box next to it. You should see a drop-down arrow. If you click the arrow, you see a list of all of the event handlers in your program that you can choose for this event. In this case, you should see the finishLabel_MouseEnter event handler that you added earlier, and the wall_MouseEnter one that you just wrote, as shown in the following picture.

    MouseEnter event with event handlers

    MouseEnter event with event handlers

  10. Select wall_MouseEnter. (If you select the wrong event handler or accidentally add a new one, you can select all of the walls and the panel again, and then choose the right method.)

  11. Now your maze game should be more fun. Try saving it and running it. If your pointer hits a wall or if you move your pointer out of the maze and back in again, the program should automatically reposition the pointer at the starting point of the maze.

To continue or review