That's what addhandler and withevents created for .
Here's an example , that uses the power of addhandler method : Addhandler can fires one or multiple events or methods at the same time . This is also called delegates. This creates new timer control and you can manage its events at runtime.
VB Code:
Dim WithEvents TimerEvent As Timer Dim timr As New Timer() Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click timr.Interval = 2000 timr.Enabled = True End Sub Private Sub FireTimer(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEvent.Tick MsgBox("Hey there") End Sub Private Sub StopTimer() timr.Enabled = False End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click AddHandler timr.Tick, AddressOf FireTimer End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click timr.Enabled = False End Sub





Reply With Quote