2012-02-09 37 views
0

我在Visual Basic.net集合(很抱歉,如果人們看到這個代碼爲九億次...)集合和變量

這裏是到目前爲止的代碼:

Public Class MainFrm 

Private _storage As New List(Of StopwatchStorage) 

Public TotalParticipants As Integer ' The total number of participants, will be set via an options form. 
Private participantLbl As Label ' A label which will hold the ordered number. 
Public participantName As TextBox ' A blank textbox to allow the user to name the participant 
Private participantClock As Label ' This is a label which will display the stopwatch 
Public ParticipantStop As Button ' A button used to the stop the timer on the participant. 
Private participantContinue As Button ' A button used to continue the timer when accidentally stopped. 
Private participantTimer As Timer ' A timer to continuously update the labels on pulses. 
Public eventName As String = "" ' The Event name itself 

' The options 
Public numEntrants As Integer = 30 ' The maximum number of participants, set at 30 by default. 
Public startingEntrants As Integer = 2 ' The number of timers to start simultaneously., set a 2 by default 
Public entryTimer As Integer = 90 ' The timer to seperate and space the entries. Set to 90 by default 
Public addAuto As Boolean = False ' A checkbox to determine whether or not to automatically add a participant. 

Dim counterTimer As Integer ' A simple holder to count down the entryTimer. 

Private participantContinueTimers As TimeSpan 
Private participantContinuation As New Stopwatch ' A Stopwatch to hold the continuation timer in the event of an error 
Private participantStopwatch As New Stopwatch 
Private matchStopwatch As New Stopwatch 

Private Sub Participant_Stop(sender As Object, args As EventArgs) 
    For Each storage As StopwatchStorage In _storage 
     If storage.Button Is sender Then 
      storage.Stopwatch.Stop() 
      storage.Button.Visible = False 
      storage.ContinueBtn.Visible = True 

      ' Reset the Continuation timer 
      storage.Continuation.Start() 
     End If 
    Next 
End Sub 


Private Sub StartButton_Click(sender As System.Object, e As System.EventArgs) Handles StartButton.Click 
    For indexCounter As Integer = 1 To startingEntrants Step 1 
     DrawControls(indexCounter) 
    Next 
End Sub 


Private Sub Participant_Resume(sender As Object, args As EventArgs) 
    For Each storage As StopwatchStorage In _storage 
     If storage.ContinueBtn Is sender Then 
      storage.ContinueBtn.Visible = False 
      storage.Button.Visible = True 
      storage.Stopwatch.Start() 
      storage.Continuation.Stop() 

      ' Add the value from storage.Continuation.Elapsed to a continuing tally 
      storage.ParticipantContinueTimers += storage.Continuation.Elapsed 
      storage.Continuation.Reset() 
     End If 
    Next 
End Sub 

Private Sub DrawControls(records As Integer) 
    participantLbl = New Label 
    participantLbl.Location = New Point(5 + (((records - 1) \ 15) * 321), 5 + (((records - 1) Mod 15) * 26)) 
    participantLbl.Size = New Size(22, 20) 
    participantLbl.TextAlign = ContentAlignment.MiddleCenter 
    participantLbl.Text = records 
    CenterPanel.Controls.Add(participantLbl) 

    participantName = New TextBox 
    participantName.Location = New Point(31 + (((records - 1) \ 15) * 321), 5 + (((records - 1) Mod 15) * 26)) 
    participantName.Size = New Size(105, 20) 
    CenterPanel.Controls.Add(participantName) 

    participantClock = New Label 
    participantClock.Size = New Size(100, 20) 
    participantClock.Name = "participantClock" & TotalParticipants 
    participantClock.Location = New Point(139 + (((records - 1) \ 15) * 321), 5 + (((records - 1) Mod 15) * 26)) 
    participantClock.BorderStyle = BorderStyle.Fixed3D 
    participantClock.TextAlign = ContentAlignment.MiddleRight 
    CenterPanel.Controls.Add(participantClock) 

    ParticipantStop = New Button 
    ParticipantStop.Size = New Size(63, 20) 
    ParticipantStop.Location = New Point(245 + (((records - 1) \ 15) * 321), 5 + (((records - 1) Mod 15) * 26)) 
    ParticipantStop.BackColor = Color.Red 
    ParticipantStop.ForeColor = Color.White 
    ParticipantStop.Font = New Font(ParticipantStop.Font, FontStyle.Bold) 
    ParticipantStop.Text = "Stop" 
    CenterPanel.Controls.Add(ParticipantStop) 
    AddHandler ParticipantStop.Click, AddressOf Participant_Stop 


    participantContinue = New Button 
    participantContinue.Size = New Size(63, 20) 
    participantContinue.Location = New Point(245 + (((records - 1) \ 15) * 321), 5 + (((records - 1) Mod 15) * 26)) 
    participantContinue.BackColor = Color.Green 
    participantContinue.ForeColor = Color.White 
    participantContinue.Font = New Font(participantContinue.Font, FontStyle.Bold) 
    participantContinue.Text = "Resume" 
    participantContinue.Visible = False 
    CenterPanel.Controls.Add(participantContinue) 
    AddHandler participantContinue.Click, AddressOf Participant_Resume 

    participantTimer = New Timer 
    participantTimer.Start() 
    participantTimer.Enabled = True 
    participantTimer.Interval = 1 
    participantStopwatch = New Stopwatch 
    participantStopwatch.Start() 


    Dim storage As New StopwatchStorage() 
    storage.Label = participantClock 
    storage.Timer = participantTimer 
    storage.Continuation = participantContinuation 
    storage.Button = ParticipantStop 
    storage.ParticipantContinueTimers = participantContinueTimers 
    storage.ContinueBtn = participantContinue 
    storage.ParticipantName = participantName 
    storage.ParticipantOrder = participantLbl 
    storage.Stopwatch = participantStopwatch 
    _storage.Add(storage) 
    AddHandler participantTimer.Tick, AddressOf Timer_Tick 

End Sub 


End Class 
Public Class StopwatchStorage 
Public Property Stopwatch As Stopwatch 
Public Property Continuation As Stopwatch 
Public Property ParticipantContinueTimers As TimeSpan 
Public Property ParticipantName As TextBox 
Public Property Label As Label 
Public Property ParticipantOrder As Label 
Public Property Timer As Timer 
Public Property Button As Button 
Public Property ContinueBtn As Button 
End Class 

什麼應該發生的是,點擊時的停止按鈕應該啓動另一個計時器/秒錶。當你點擊恢復按鈕時,秒錶的值應該加回到正在運行的計時器上。 (它被添加到`participantContinueTimers然後加回到正在運行的計時器上。)現在代碼正在做它應該做的事情,但是,而不是添加那個停止的時鐘的特定,它添加了所有的停止點擊然後重置。

有人可以解釋我怎麼能得到這個工作,以便只有一個停止的結果被添加?

編輯:添加存儲的方法。任何更多的代碼,我將不得不託管它在SourceForge或Codeplex上,並從那裏工作。

+0

我還沒有理解這個問題本身。你能否更詳細地解釋下面的內容:「而不是添加一個停止時鐘的特定時間,它會添加停止點擊的所有值然後重置。」 – 2012-02-09 16:18:21

+0

該程序是動態創建控件。兩個秒錶製成。一個在創建時運行(例如TimerA),一個在創建的時候停止運行(定時器B)。應該發生的事情是,通過點擊繼續按鈕,定時器B的值應該被添加回到定時器A,然後定時器A應該再次運行。這應該發生在那個特定的計時器上。如果我製作30個定時器,請停止其中的29個,然後嘗試恢復其中任何一個29,而不是僅添加一個TimerB的值,而是將所有TimerB相加。 – 2012-02-09 16:23:00

+0

您尚未顯示如何創建這些StopwatchStorages。所以也許你正在使用相同的引用來引發這種行爲。 – 2012-02-09 16:31:22

回答

1

您不應該爲動態控件使用成員變量。通過這種方式,您將覆蓋所有控件,只有最後一個控件才能勝出。相反,您應該分別在方法中創建它們。您還在使用參數records,但我沒有看到代碼將其循環以創建多個StopwatchStorage

Private Sub DrawControls(records As Integer) 
    Dim participantLbl = New Label 
    participantLbl.Location = New Point(5 + (((records - 1) \ 15) * 321), 5 + (((records - 1) Mod 15) * 26)) 
    participantLbl.Size = New Size(22, 20) 
    participantLbl.TextAlign = ContentAlignment.MiddleCenter 
    participantLbl.Text = records 
    CenterPanel.Controls.Add(participantLbl) 

    Dim participantName = New TextBox 
    participantName.Location = New Point(31 + (((records - 1) \ 15) * 321), 5 + (((records - 1) Mod 15) * 26)) 
    participantName.Size = New Size(105, 20) 
    CenterPanel.Controls.Add(participantName) 

    Dim participantClock = New Label 
    participantClock.Size = New Size(100, 20) 
    participantClock.Name = "participantClock" & TotalParticipants 
    participantClock.Location = New Point(139 + (((records - 1) \ 15) * 321), 5 + (((records - 1) Mod 15) * 26)) 
    participantClock.BorderStyle = BorderStyle.Fixed3D 
    participantClock.TextAlign = ContentAlignment.MiddleRight 
    CenterPanel.Controls.Add(participantClock) 

    Dim ParticipantStop = New Button 
    ParticipantStop.Size = New Size(63, 20) 
    ParticipantStop.Location = New Point(245 + (((records - 1) \ 15) * 321), 5 + (((records - 1) Mod 15) * 26)) 
    ParticipantStop.BackColor = Color.Red 
    ParticipantStop.ForeColor = Color.White 
    ParticipantStop.Font = New Font(ParticipantStop.Font, FontStyle.Bold) 
    ParticipantStop.Text = "Stop" 
    CenterPanel.Controls.Add(ParticipantStop) 
    AddHandler ParticipantStop.Click, AddressOf Participant_Stop 


    Dim participantContinue = New Button 
    participantContinue.Size = New Size(63, 20) 
    participantContinue.Location = New Point(245 + (((records - 1) \ 15) * 321), 5 + (((records - 1) Mod 15) * 26)) 
    participantContinue.BackColor = Color.Green 
    participantContinue.ForeColor = Color.White 
    participantContinue.Font = New Font(participantContinue.Font, FontStyle.Bold) 
    participantContinue.Text = "Resume" 
    participantContinue.Visible = False 
    CenterPanel.Controls.Add(participantContinue) 
    AddHandler participantContinue.Click, AddressOf Participant_Resume 

    Dim participantTimer = New Timer 
    participantTimer.Start() 
    participantTimer.Enabled = True 
    participantTimer.Interval = 1 
    participantStopwatch = New Stopwatch 
    participantStopwatch.Start() 


    Dim storage As New StopwatchStorage() 
    storage.Label = participantClock 
    storage.Timer = participantTimer 
    storage.Continuation = participantContinuation 
    storage.Button = ParticipantStop 
    storage.ParticipantContinueTimers = participantContinueTimers 
    storage.ContinueBtn = participantContinue 
    storage.ParticipantName = participantName 
    storage.ParticipantOrder = participantLbl 
    storage.Stopwatch = participantStopwatch 
    _storage.Add(storage) 
    AddHandler participantTimer.Tick, AddressOf Timer_Tick 
End Sub 
+0

我編輯了我的回覆以顯示我如何循環創建存儲。特別的功能:StartButton_Click – 2012-02-09 17:27:10

+0

@Paul:那麼我的答案能解決你的問題嗎?正如我所說,你正在創建'n' StopWatchStorages,但所有引用相同的控件。 – 2012-02-09 17:39:30

+0

不幸的是。我仍然有同樣的問題。除非你的代碼沒有改變任何東西,它看起來仍然是我的。 – 2012-02-09 17:43:32