【问题标题】:How do I update a Listbox Index with a Timer output如何使用计时器输出更新列表框索引
【发布时间】:2017-06-03 12:30:36
【问题描述】:

我对使用 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

【问题讨论】:

  • 我很困惑。请详细说明!您的列表框中会有多个条目/行吗?哪些行及其计数器需要更新。解释“t”值的含义。
  • 列表框会有多行。 t 值按从左到右的顺序表示文本框(见图:i.stack.imgur.com/aFWRX.png)和它们的输入。 t1 - t2 - t3 - t4 - count.text 示例:Hello - 3 - David - Description - "Timer"

标签: vb.net vba timer listbox listboxitem


【解决方案1】:

仍然不确定我是否理解正确......但尝试从以下开始:

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

【讨论】:

  • 这是我需要的。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
  • 2017-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多