vb.net 如何创建运行时 timer.tick 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22215407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:59:24  来源:igfitidea点击:

How to create a runtime timer.tick event?

vb.nettimer

提问by SmashedPotato

i'm really bothered on how to create a timer.tick event on VB.net.

我真的很困扰如何在 VB.net 上创建 timer.tick 事件。

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. :(

实际上,我正在创建一个 FCFS 算法,我的目标是使用进度条显示甘特图。关于这一点,我希望我的计时器控制给定的进度条。在进度条达到最大值后,计时器将停止,下一个计时器将启动,下一个进度条也会起作用。我真的不知道该怎么做。我是新手。请帮我。:(

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

采纳答案by Mark Hall

You would use the AddHandlerMethod.

您将使用AddHandler方法。

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