程序需要跟踪玩家单击了哪些 Label 控件。在第一个标签被单击后,程序显示该标签的图标。在第二个标签被单击后,程序需要同时显示两个图标很短的时间,然后再次使这两个图标不可见。程序将使用引用变量跟踪第一次和第二次分别单击了哪个 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 Dim firstClicked As Label = Nothing ' secondClicked points to the second Label control ' that the player clicks Dim 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 对象。
说明
当某变量不跟踪对象时,它设置为特殊的值:null(Visual C# 中)或 Nothing(Visual Basic 中)。因此,当程序启动时,firstClicked 和 secondClicked 都设置为 null 或 Nothing,这意味着该变量不会跟踪任何内容。
修改 Click 事件处理程序,以使用新的 firstClicked 引用变量。移除 label_Click() 事件处理程序方法中的最后一个语句 (clickedLabel.ForeColor = Color.Black;),并将它替换为下面的 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 As Label = 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 Return End If ' 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 Return 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 语句中执行语句。因此,只有被单击的第一个图标变为黑色,其他图标是不可见的,如下图所示。
显示一个图标的匹配游戏
继续或查看
若要转到下一个教程步骤,请参见步骤 6:添加计时器。
若要返回上一个教程步骤,请参见步骤 4:向每个标签添加一个 Click 事件处理程序。