Step 5: Add Label References

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

The program needs to track which label controls the player chooses. Right now, the program shows all labels chosen by the player. But we're going to change that. After the first label is chosen, the program should show the label's icon. After the second label is chosen, the program should display both icons for a brief time, and then hide both icons again. Your program will now keep track of which label control is chosen first and which is chosen second by using reference variables.

To add label references

  1. Add label references to your form by using the following code.

    public partial class Form1 : Form
    {
        // firstClicked points to the first Label control 
        // that the player clicks, but it will be null 
        // if the player hasn't clicked a label yet
        Label firstClicked = null;
    
        // secondClicked points to the second Label control 
        // that the player clicks
        Label secondClicked = null;
    
    Public Class Form1
    
        ' firstClicked points to the first Label control 
        ' that the player clicks, but it will be Nothing 
        ' if the player hasn't clicked a label yet
        Private firstClicked As Label = Nothing
    
        ' secondClicked points to the second Label control 
        ' that the player clicks
        Private secondClicked As Label = Nothing
    

    These reference variables look similar to the statements you used earlier to add objects (like Timer objects, List objects, and Random objects) to your form. However, these statements don't cause two extra label controls to appear on the form because there's no new keyword used in either of the two statements. Without the new keyword, no object is created. That's why firstClicked and secondClicked are called reference variables: They just keep track (or, refer to) Label objects.

    When a variable isn't keeping track of an object, it's set to a special reserved value: null in Visual C# and Nothing in Visual Basic. So, when the program starts, both firstClicked and secondClicked are set to null or Nothing, which means that the variables aren't keeping track of anything.

  2. Modify your Click event handler to use the new firstClicked reference variable. Remove the last statement in the label_Click() event handler method (clickedLabel.ForeColor = Color.Black;) and replace it with the if statement that follows. (Be sure you include the comment, and the whole if statement.)

    /// <summary>
    /// Every label's Click event is handled by this event handler
    /// </summary>
    /// <param name="sender">The label that was clicked</param>
    /// <param name="e"></param>
    private void label_Click(object sender, EventArgs e)
    {
        Label clickedLabel = sender as Label;
    
        if (clickedLabel != null)
        {
            // If the clicked label is black, the player clicked
            // an icon that's already been revealed --
            // ignore the click
            if (clickedLabel.ForeColor == Color.Black)
                return;
    
            // If firstClicked is null, this is the first icon 
            // in the pair that the player clicked,
            // so set firstClicked to the label that the player 
            // clicked, change its color to black, and return
            if (firstClicked == null)
            {
                firstClicked = clickedLabel;
                firstClicked.ForeColor = Color.Black;
    
                return;
            }
        }
    }
    
    ''' <summary>
    ''' Every label's Click event is handled by this event handler
    ''' </summary>
    ''' <param name="sender">The label that was clicked</param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub label_Click(ByVal sender As System.Object, 
                            ByVal e As System.EventArgs) Handles Label9.Click, 
        Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, 
        Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, 
        Label13.Click, Label12.Click, Label11.Click, Label10.Click, Label1.Click
    
        Dim clickedLabel = TryCast(sender, Label)
    
        If clickedLabel IsNot Nothing Then
    
            ' If the clicked label is black, the player clicked
            ' an icon that's already been revealed --
            ' ignore the click
            If clickedLabel.ForeColor = Color.Black Then Exit Sub
    
            ' If firstClicked is Nothing, this is the first icon 
            ' in the pair that the player clicked, 
            ' so set firstClicked to the label that the player
            ' clicked, change its color to black, and return
            If firstClicked Is Nothing Then
                firstClicked = clickedLabel
                firstClicked.ForeColor = Color.Black
                Exit Sub
            End If
        End If
    
    End Sub
    
  3. Save and run your program. Choose one of the label controls, and its icon appears.

  4. Choose the next label control, and notice that nothing happens. The program is already keeping track of the first label that the player chose, so firstClicked isn't equal to null in Visual C# or Nothing in Visual Basic. When your if statement checks firstClicked to determine if it's equal to null or Nothing, it finds that it isn't, and it doesn't execute the statements in the if statement. So, only the first icon that's chosen turns black, and the other icons are invisible, as shown in the following picture.

    Matching game showing one icon Matching game showing one icon

    You'll fix this situation in the next step of the tutorial by adding a Timer control.

To continue or review