程式需要追蹤玩家所選擇的 Label 控制項。現在,程式會顯示玩家選擇的所有標籤。但是,我們將要變更該行為。在選擇第一個標籤之後,程式應該會顯示標籤的圖示。在選擇第二個標籤之後,程式應該要短暫顯示這兩個圖示,然後再次隱藏這兩個圖示。您的程式現在將會使用「參考變數」(Reference Variable),追蹤第一次和第二次選擇的 Label 控制項。
若要加入標籤參考
使用下列程式碼,將標籤參考加入至您的表單。
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 = Nothingpublic 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,這表示變數並未追蹤任何項目。
修改 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; } } }儲存並執行您的程式。選擇其中一個 Label 控制項,其圖示就會出現。
選擇一個 Label 控制項,並注意什麼事也沒發生。程式已經在追蹤玩家所選擇的第一個標籤,所以 firstClicked 不等於 Visual C# 中的 null 或 Visual Basic 中的 Nothing。當 if 陳述式檢查 firstClicked 以判斷它是否等於 null 或 Nothing 時,若發現不相等,就不會執行 if 陳述式中的陳述式。因此只有選擇的第一個圖示會變成黑色,而其他圖示則看不見,如下列圖片所示。
顯示一個圖示的配對遊戲
.png)
您可以在教學課程的下一個步驟中加入 [Timer] 控制項,以解決此問題。
若要繼續或檢視
若要移到下一個教學課程步驟,請參閱步驟 6:加入計時器。
若要回到上一個教學課程步驟,請參閱步驟 4:將 Click 事件處理常式加入至每個標籤。