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

标签 vb.net timer

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

我的程序应该如何工作:

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

这是我的代码:

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

最佳答案

您将使用 AddHandler方法。

timex.Add(New Timer)
AddHandler timex(cnt4).Tick, AddressOf myTickEvent

然后您将创建您的事件。

Private Sub myTickEvent(sender As Object, e As EventArgs)
    Dim instance As Integer = CInt(DirectCast(sender, Timer).Tag)

    'Do your magic here
End Sub

用一个工作示例来充实它:

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

关于vb.net - 如何创建运行时 timer.tick 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22215407/

相关文章:

c# - View 模型中的计时器

timer - 在blazor服务器中使用PeriodicTimer刷新组件的最佳方法是什么

asp.net - 本地化:如果在切换文化时 URL 没有改变,SE 是否认为它是重复内容?

vb.net - 如何在 Word 文档中插入 Image 对象作为图片

vb.net - 控制台应用程序在现实生活中的使用

c# - 如何使用计时器每 x 秒更改一次标签文本

c - Linux 计时器 CLOCK_PROCESS_CPU_ID 不工作

vb.net - 有什么方法可以暂停 Windows 关闭,以便 forms.closing 方法可以完成吗?

javascript - (VB) 通过 javascript 使用 Url.Action 传递文本框值

swift - 定时器只执行一次函数