关于timer控件的一点疑问
我只想让time_tick里的函数运行一次,我这样做,定义一个全局boolean变量a,初始为true,timer的interval设为10000,也就是10秒,这样写
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If a = True Then MsgBox("safalj") a = False End If End SubModule Module1 Public a As Boolean = FalseEnd ModulePublic Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim f2 As New Form2 f2.Show() End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click a = True End SubEnd ClassPublic Class Form2 Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick If a = True Then MsgBox("safalj") a = False End If End SubEnd Class
[解决办法]
1. 定义静态变量:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static a As Boolean
My.Application.DoEvents()
If a = False Then
a = True
MsgBox("a")
End If
End Sub
2.定义全局变量
就像8楼那样...
楼主基础有待加强