【问题标题】:button in vb application pressed itself every x of time in loop after first click [closed]vb应用程序中的按钮在第一次单击后每x次循环按下[关闭]
【发布时间】:2018-10-08 15:24:21
【问题描述】:

这是我的按钮,只需要知道我对 vb.net 很陌生

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

【问题讨论】:

标签: vb.net


【解决方案1】:

最好在按钮点击中调用方法,而不是将代码放入其中 - 特别是如果您希望“每 x 次”执行一次点击。

Dim timer As New System.Threading.Timer(AddressOf doClickStuff)
Dim interval As Integer = 1000 ' 1000 ms = 1 second

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    doClickStuff()
End Sub

Private Sub doClickStuff()
    If Me.InvokeRequired Then
        Me.Invoke(New Action(AddressOf doClickStuff))
    Else
        ' do stuff here
        timer.Change(interval, -1)
    End If
End Sub

'' if you won't access UI elements in "do stuff here"
'' you can use this method which will run on a non-UI thread
'Private Sub doClickStuff()
'    ' do stuff here
'    timer.Change(interval, -1)
'End Sub

' stops the timer
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    timer.Change(-1, -1)
End Sub

【讨论】:

  • 非常感谢,它运行得很好,没有错误,谢谢兄弟跟上
猜你喜欢
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
相关资源
最近更新 更多