Step 5: Add Label References

The program needs to keep track of which Label controls the player clicks. After the first label is clicked, the program shows the label's icon. After the second label is clicked, the program needs to show both icons for a brief time, and then make the icons invisible again. Your program will keep track of which Label control is clicked first and which is clicked second by using reference variables.

To add label references

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

    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
    
    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;
    

    Note

    The reference variables look similar to the statements you used 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 in either of the two statements. Without new, no object is created. That's why firstClicked and secondClicked are called reference variables: They just keep track (or refer to) Label objects.

    Note

    When a variable isn't keeping track of an object, it's set to a special 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> 
    ''' <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
    
    /// <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;
            }
        }
    }
    
  3. Save and run your program. Click one of the Label controls, and its icon appears.

  4. Click the next Label control, and notice that nothing happens. The program is already keeping track of the first label that the player clicked, 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 clicked turns black, and the other icons are invisible, as shown in the following picture.

    Matching game showing one icon

    Matching game showing one icon

To continue or review