共用方式為


步驟 5:加入標籤參考

程式需要追蹤玩家所按下的 Label 控制項。 在按下第一個標籤之後,程式會顯示標籤的圖示。 在按下第二個標籤之後,程式需要短暫顯示這兩個圖示,然後讓圖示再度看不見。 您的程式會使用參考變數,追蹤第一次和第二次按下的 Label 控制項。

若要加入標籤參考

  1. 使用下列程式碼,將標籤參考加入至您的表單。

    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;
    
    注意事項注意事項

    參考變數看起來類似您用來將物件 (例如 Timer 物件、List 物件及 Random 物件) 加入至表單的陳述式。但是,這些陳述式不會導致表單上出現兩個額外的 Label 控制項,因為這兩個陳述式中都沒有 new。若沒有 new,就不會建立物件。這就是為何 firstClicked 和 secondClicked 都稱為參考變數:它們只會追蹤 (或參照) Label 物件。

    注意事項注意事項

    當變數未追蹤物件時,該變數會設為特殊值:在 Visual C# 中為 null,而在 Visual Basic 中為 Nothing。因此當程式啟動時,firstClicked 和 secondClicked 會設為 null 或 Nothing,這表示變數並未追蹤任何項目。

  2. 修改 Click 事件處理常式,以使用新的 firstClicked 參考變數。 移除 label_Click() 事件處理常式方法 (clickedLabel.ForeColor = Color.Black;) 中的最後一個陳述式,並以後面的 if 陳述式取代 (請務必在包含註解,並將整個if陳述式。)

    ''' <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. 儲存並執行您的程式。 按一下其中一個 Label 控制項,其圖示就會出現。

  4. 按下一個 Label 控制項,並注意什麼事也沒發生。 程式已經在追蹤玩家所按下的第一個標籤,所以 firstClicked 不等於 Visual C# 中的 null 或 Visual Basic 中的 Nothing。 當 if 陳述式檢查 firstClicked 以判斷它是否等於 null 或 Nothing 時,若發現不相等,就不會執行 if 陳述式中的陳述式。 因此只有按下的第一個圖示會變成黑色,而其他圖示則看不見,如下列圖片所示。

    顯示一個圖示的配對遊戲

    顯示一個圖示的配對遊戲

若要繼續或檢視