次の方法で共有


手順 5: ラベルの参照の追加

プログラムでは、プレーヤーがどの Label コントロールをクリックしたかを追跡する必要があります。1 つ目のラベルがクリックされると、プログラムではラベルのアイコンが表示されます。2 つ目のラベルがクリックされると、プログラムでは、一時的に両方のアイコンを表示した後に、再びアイコンを非表示にする必要があります。プログラムでは、参照変数を使用して、1 回目および 2 回目にどの 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
        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 オブジェクトなど) を追加するために使用するステートメントに似ているように見えます。ただし、これらのステートメントでは、フォームに追加の 2 つの Label コントロールは表示されません。これは、2 つのステートメントには new がないためです。new がない場合、オブジェクトは作成されません。firstClicked および secondClicked が参照変数と呼ばれるのはこのためです。これらは、単に Label オブジェクトを追跡 (参照) するだけです。

    注意

    変数は、オブジェクトを追跡していない間は、特殊な値 null (Visual C# の場合) および Nothing (Visual Basic の場合) に設定されます。そのため、プログラムが起動されると、firstClicked および secondClicked の両方が null または Nothing に設定されます。これは、変数が何も追跡していないことを意味しています。

  2. 新しい firstClicked 参照変数を使用するように Click イベント ハンドラーを変更します。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;
            }
        }
    }
    
  3. プログラムを保存し、実行します。いずれかの Label コントロールをクリックすると、そのアイコンが表示されます。

  4. 次の Label コントロールをクリックすると、何も起こらないことに気付きます。プログラムでは、プレーヤーがクリックした 1 つ目のラベルが既に追跡されているため、firstClicked は null (Visual C# の場合) または Nothing (Visual Basic の場合) ではありません。if ステートメントは、firstClicked が null または Nothing かどうかをチェックし、そうでないことがわかった場合は、if ステートメント内のステートメントを実行しません。そのため、次の図に示すように、クリックされた 1 つ目のアイコンのみが黒になり、他のアイコンは非表示になります。

    1 つのアイコンが表示された絵合わせゲーム

    1 つのアイコンが表示された絵合わせゲーム

続行または確認するには