如何使用Timer输出更新列表框索引

时间:2017-06-03 12:30:37

标签: vb.net vba timer listbox listboxitem

我对Visual Basics编码相当新。我正在处理一个项目,我需要在列表中创建多个项目,并且每个列表的末尾都有一个计时器,当按下“开始”按钮时,它们会单独计数。 目前的问题是,当计时器增加计数时,我无法更新索引。 Form Design Picture Here 我在下面列出了我的代码。任何有关如何实现这一目标的反馈都会有很大帮助。

Public Class Form1

    Dim t1, t2, t3, t4 As Object
    Dim count As Object

    Public Sub GlobalTimerTick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
        count.Text = Val(count.Text) + 1
        ListBox1.Update()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        count = New System.Windows.Forms.Label
        count.Text = 0
        t1 = TextBox1.Text
        t2 = TextBox2.Text
        t3 = TextBox3.Text
        t4 = TextBox4.Text
        ListBox1.Items.Add("P" + t1 + " - " + t2 + " - " + t3 + " - " + t4 + " - " + count.text)

        If (t1 = "") Then
            MsgBox("Please enter a P-Level number.")
        ElseIf (t1 = 0) Then
            Timer1.Start()
        ElseIf (t1 = 1) Then

        ElseIf (t1 = 2) Then

        ElseIf (t1 = 3) Then

        ElseIf (t1 = 4) Then
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ListBox1.Items.Remove(ListBox1.SelectedItem)
        Timer1.Stop()
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

仍不确定我是否理解正确...但请尝试以下开头:

Public Class Form1

    Private Entries As New System.ComponentModel.BindingList(Of Entry)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.DataSource = Entries
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ent As New Entry
        ent.T1 = TextBox1.Text
        ent.T2 = TextBox2.Text
        ent.T3 = TextBox3.Text
        ent.T4 = TextBox4.Text
        Entries.Add(ent)
    End Sub

    Private Class Entry

        Public T1 As String
        Public T2 As String
        Public T3 As String
        Public T4 As String
        Public Shared Counter As Integer

        Private Shared Delimiter As String = " - "

        Public Overrides Function ToString() As String
            Return "P" & T1 & Delimiter & T2 & Delimiter & T3 & Delimiter & T4 & Delimiter & Counter
        End Function

    End Class

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Entry.Counter = Entry.Counter + 1
        ListBox1.DataSource = Nothing
        ListBox1.DataSource = Entries
    End Sub

End Class