Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 集合和变量_Vb.net_Visual Studio 2010_Collections_Stopwatch - Fatal编程技术网

Vb.net 集合和变量

Vb.net 集合和变量,vb.net,visual-studio-2010,collections,stopwatch,Vb.net,Visual Studio 2010,Collections,Stopwatch,我在visualbasic.net中有一个集合(如果人们已经看到这段代码90亿次了,那就很抱歉了…) 以下是迄今为止的代码: 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 p

我在visualbasic.net中有一个集合(如果人们已经看到这段代码90亿次了,那就很抱歉了…)

以下是迄今为止的代码:

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上,然后从那里开始工作。

您不应该将成员变量用于动态控件。这样,您将覆盖所有控件,只有最后一个控件获胜。相反,您应该在方法中单独创建它们。您还使用了一个参数
records
,但我看不到您循环它以创建多个
秒表存储的代码

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

我不明白这个问题本身。你能再解释一下吗:“它不是添加一个停止的时钟的特定值,而是添加停止的单击的所有值,然后重置。”程序正在动态创建控件。制造了两块秒表。一个在创建时运行(例如TimerA),另一个在创建的停止时运行(计时器B)。应该发生的是,通过点击“继续”按钮,计时器B的值应该被添加回计时器a,然后计时器a应该再次运行。这应该只发生在一个特定的计时器上。如果我制作了30个计时器,停止其中的29个,然后尝试恢复其中的任何一个,而不是仅仅添加一个TimerB的值,而是将所有TimerB的值相加。您还没有展示如何创建这些秒表存储。因此,可能您对导致这种行为的某些内容使用了相同的引用。请参见(我认为)存储的制作方式。不确定我是否可以在不发布整个表单的情况下发布更多代码。我已编辑了我的回复,以显示我如何循环创建存储。具体功能:开始按钮_Click@Paul:那么我的回答能解决你的问题吗?正如我所说,您正在创建
n
stopwatch存储,但都引用了相同的控件。不幸的是,这并不正确。我仍然有同样的问题。除非你的代码没有改变任何东西,否则它看起来还是我的。不,我正在本地创建方法中的控件,例如:
Dim participantLbl=New Label
。您正在初始化类范围内声明的实例变量,例如:
participantLbl=New Label
。您的代码将只使用一个引用(对于每个控件),我的代码将创建具有不同引用的
n
控件。好的,我现在看到了。但不,我仍然有同样的结果。那么,我应该在表单的开头去掉声明吗?