如何制作一个只能点击一次的按钮

时间:2016-11-15 23:30:30

标签: vba

在visual basic中我试图制作一个只能点击一次的按钮,我希望能够看到按钮,它只是我想要它所以你只能点击它一次。

到目前为止,这是我的代码:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub
End Class

2 个答案:

答案 0 :(得分:2)

点击按钮后禁用该按钮。然后他们将无法再点击它,但它仍然可见。确保首先禁用该按钮,尤其是在事件代码是多线程的情况下。否则,它可能会为用户提供在您需要之前再次单击该按钮的机会。

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs)
        Button1.Enabled = False
        ' Do something
    End Sub
End Class

如果您不想禁用该按钮,另一种方法是设置一个全局(最好是私有)变量来跟踪它。

Public Class Form1
    Private button1Clicked As Boolean = False
    Private Sub Button1_Click(sender As Object, e As EventArgs)
        If button1Clicked Then
           ' Optionally inform user they've already clicked on it.
           MessageBox.Show("You've already clicked on the button.")
        Else
            button1Clicked = True
            ' Do something
        End If
    End Sub
End Class

或者您可以使用Tag属性。

Private Sub Button1_Click(sender As Object, e As EventArgs)
    If Button1.Tag = True Then 
       ' Optionally inform user they've already clicked on it.
       MessageBox.Show("You've already clicked on the button.")
    Else
        Button1.Tag = True
        ' Do something
    End If
End Sub

答案 1 :(得分:0)

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs)
   'Do what you need to do here, and then 
   Button1.Enabled = false
End Sub