2017-06-03 41 views
1

我對使用Visual Basics進行編碼相當新穎。我正在開發一個項目,我需要在列表中創建多個項目,並且每個列表的末尾都有一個計時器,這些計時器在按下「開始」按鈕時單獨計數。 目前的問題是,當定時器增加計數時,我一直無法更新索引。 Form Design Picture Here 我在下面列出了我的代碼。任何有關如何實現這一點的反饋都會有很大的幫助。如何使用定時器輸出更新Listbox索引

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

我很困惑。請更詳細地解釋!你的ListBox中會有多個條目/行嗎?哪些線與他們的櫃檯需要更新。解釋「t」值是怎麼回事。 –

+0

列表框將有多行。 t值從左至右依次代表文本框(見圖片https://i.stack.imgur.com/aFWRX.png)及其輸入。 t1 - t2 - t3 - t4 - count.text 示例:Hello - 3 - David - 描述 - 「Timer」 – AdamO

回答

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 
+0

這就是我所需要的。謝謝 – AdamO