寻找一种方法来重新启用标签,而无需指定哪些vb.net

时间:2014-02-22 23:58:41

标签: vb.net properties onclick label

我正在寻找一种方法,如果可能的话,重新启用标签而不需要实际使用其标签名称?

我有一个带有标签的游戏​​我用作可点击的框。单击事件发生后,第一个框将被禁用,我想在单击第二个框后重新启用第一个框。有任何想法吗?以下是前两个框点击事件的代码。

编辑:将有15个标签,一次可以选择2个。第一个将被禁用,以便第二次无法选择。

    Private Sub lblMemory1_Click(sender As Object, e As EventArgs) Handles lblMemory1.Click

    Dim intClickBox As Integer = 1
    Dim intClickAnswer As Integer

    intClickAnswer = GuessClick(intClickBox)



    If blnActive = False Then
        blnActive = True
        whatClicked1 = intClickBox
        lblMemory1.BackColor = Color.Green
        lblMemory1.Text = intClickAnswer.ToString
        lblMemory1.Refresh()
        System.Threading.Thread.Sleep(2000)
        lblMemory1.BackColor = Color.Cyan
        lblMemory1.Text = "X"
        lblMemory1.Enabled = False
    End If

    If blnActive = True Then
        whatClicked2 = intClickBox
        lblMemory1.BackColor = Color.Green
        lblMemory1.Text = intClickAnswer.ToString
        lblMemory1.Refresh()
        System.Threading.Thread.Sleep(2000)
        lblMemory1.BackColor = Color.Cyan
        lblMemory1.Text = "X"
        blnActive = False
    End If


End Sub

Private Sub lblMemory2_Click(sender As Object, e As EventArgs) Handles lblMemory2.Click

    Dim intClickBox As Integer = 2
    Dim intClickAnswer As Integer

    intClickAnswer = GuessClick(intClickBox)



    If blnActive = False Then
        blnActive = True
        whatClicked1 = intClickBox
        lblMemory2.BackColor = Color.Green
        lblMemory2.Text = intClickAnswer.ToString
        lblMemory2.Refresh()
        System.Threading.Thread.Sleep(2000)
        lblMemory2.BackColor = Color.Cyan
        lblMemory2.Text = "X"
        lblMemory2.Enabled = False
    End If

    If blnActive = True Then
        whatClicked2 = intClickBox
        lblMemory2.BackColor = Color.Green
        lblMemory2.Text = intClickAnswer.ToString
        lblMemory2.Refresh()
        System.Threading.Thread.Sleep(2000)
        lblMemory2.BackColor = Color.Cyan
        lblMemory2.Text = "X"
        blnActive = False
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

你可以做点什么

    Public Sub game(sender As Object)
        Dim lbl As Label = sender
        If lbl.Enabled Then
            For i = 0 To Me.Controls.Count - 1
                Dim lbl2 As Label = Me.Controls(i)
                If Not (lbl2.Enabled) Then
                    lbl2.Enabled = True
                    GoTo exitLoop
                End If
            Next
        End If
exitLoop:
        lbl.Enabled = False
    End Sub

然后在每个标签中只需调用游戏子并像这样传递发件人......

Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles Label1.Click
    game(sender)
End Sub

这将允许在任何给定时间只有1个标签被禁用... 你也可以(在循环内)使用lbl和lbl2来设置每个标签的属性。 如果您使用var来保存第一个文本框的名称,您还可以专门重新启用该框... 如果您也想要代码,请告诉我。