How to use a Timer control to trigger a button click event?

candice

Active member
Joined
Jan 23, 2007
Messages
30
Programming Experience
Beginner
hello guys,
I now wanna use a timer control to trigger the button click event every 10 seconds.
I've already added a timer control to the form and set the Enable to true, the time interval to 10000.
My buttonclick procedure's code is like this:
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click_1([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click[/SIZE]
[SIZE=2]' code goes on here[/SIZE]
[SIZE=2]End Sub
[/SIZE]
And the time trigger procedure is like this
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] t_Tick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] t.Tick
Console.WriteLine([/SIZE][SIZE=2][COLOR=#800000]"d"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]RaiseEvent[/COLOR][/SIZE][SIZE=2] ButtonClick([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2], e)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

The t_Tick prodedure works well because the "d" is written every 10 sec as expected. But the RaiseEvent statement is wrong I guess. Could anybody tell me that what's the write code to Raise the button click event?
Thanks a lot!
 
VB.NET:
Expand Collapse Copy
PrivateSub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    method()
End Sub
 
Private Sub t_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles t.Tick
    method()
End Sub
 
Sub method()
    ' code goes on here
End sub

You can also call Button1.PerformClick()
 
Back
Top