How to create a runtime timer.tick event?
10,167
You would use the AddHandler Method.
timex.Add(New Timer)
AddHandler timex(cnt4).Tick, AddressOf myTickEvent
You would then create your event.
Private Sub myTickEvent(sender As Object, e As EventArgs)
Dim instance As Integer = CInt(DirectCast(sender, Timer).Tag)
'Do your magic here
End Sub
To flesh it out more with an working example:
Public Class Form1
Private progress As New List(Of ProgressBar)
Private timex As New List(Of Timer)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For cnt4 = 0 To (Val(TextBox1.Text) - 1)
progress.Add(New ProgressBar)
With progress(cnt4)
.Parent = Me
.Left = 0
.Height = 23
.Width = 50
.Top = .Height * cnt4 + 50
.Visible = True
.Tag = cnt4
.Text = ""
.Maximum = Val(burstbox(cnt4).Text) 'Set Maximum
.Minimum = 0
.Name = "progress" & cnt4
.Location = New Point(17 + (.Width * cnt4), 532)
Me.Controls.Add(progress(cnt4)) 'Have to add it to your Containers Control Collection
End With
timex.Add(New Timer)
AddHandler timex(cnt4).Tick, AddressOf myTickEvent
With timex(cnt4)
.Tag = cnt4
.Interval = 100
End With
Next
timex(0).Start()
End Sub
Private Sub myTickEvent(sender As Object, e As EventArgs)
Dim instance As Integer = CInt(DirectCast(sender, Timer).Tag) 'Get Index of the Active Timer
If progress(instance).Value >= progress(instance).Maximum Then
timex(instance).Stop()
RemoveHandler timex(instance).Tick, AddressOf myTickEvent
If instance < progress.Count - 1 Then
timex(instance + 1).Start()
End If
Else
progress(instance).Value += 1
End If
End Sub
End Class
Author by
SmashedPotato
Updated on June 05, 2022Comments
-
SmashedPotato almost 2 years
i'm really bothered on how to create a timer.tick event on VB.net.
How my program should work:
Actually, i'm creating an FCFS algorithm and im aiming to display the gantt chart using progressbars. Regarding that, i want my timers to control a given progressbar. and after a progressbar reached its maximum, the timer will stop and the next timer will start and the next progressbar will function too. I really dont know how to do this. Im a newbie. please help me. :(
This is my code:
Private progress As New List(Of ProgressBar) Private timex As New List(Of Timer) For cnt4 = 0 To (Val(TextBox1.Text) - 1) progress.Add(New ProgressBar) With progress(cnt4) .Parent = Me .Left = 0 .Height = 23 .Width = 50 .Top = .Height * cnt4 + 50 .Visible = True .Tag = cnt4 .Text = "" .Maximum = Val(burstbox(cnt4).Text) .Minimum = 0 .Name = "progress" & cnt4 .Location = New Point(17 + (.Width * cnt4), 532) End With timex.Add(New Timer) With timex(cnt4) .Tag = cnt4 .Interval = 100 End With Next End Sub
-
SmashedPotato about 10 yearsAddHandler Method? How?
-
Mark Hall about 10 yearsI just hardcoded the value for a demonstration, you do not need to use a for loop the value is already there as part of your your Progress Control, see the edit I made a few minutes ago.
-
SmashedPotato about 10 yearsSir what if i want to change the maximum value of the progressbar depending on a particular burst?
-
Mark Hall about 10 yearsYou are doing it in your original code by setting your maximum. I modified the Tick Event to test for the ProgressBar's maximum that was set when you created it.
-
SmashedPotato about 10 yearsSay burstbox(1) has a value of 10. then, progressbar(1).maximum will be burstbox(1)'s value.