选择最近使用 VB.NET 动态创建的对象

发布于 2025-01-02 01:19:58 字数 1500 浏览 1 评论 0原文

下面是我用 Visual Basic 创建对象的一些代码:

    For indexCounter As Integer = 1 To TotalParticipants Step 1

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

        participantStop = New Button
        participantStop.Size = New Size(58, 20)
        participantStop.Location = New Point(245, (5 + ((indexCounter - 1) * 26)))
        participantStop.BackColor = Color.Red
        participantStop.ForeColor = Color.White
        participantStop.Font = New Font(participantStop.Font, FontStyle.Bold)
        participantStop.Text = "Stop"
        CenterPanel.Controls.Add(participantStop)

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

        participantStopwatch = New Stopwatch
        participantStopwatch.Start()
Next

我正在创建一个标签、一个按钮、计时器和秒表。 (虽然我有一种沉闷的感觉,但我不需要计时器和秒表,因为我正在计算时间。)

我想做的是创建标签并将该标签的文本设置为秒表中的值。将创建的按钮将停止该秒表。

我遇到的问题是我无法通过名称调用秒表,因为它尚未创建,并且 VB 对此发出了嘶嘶声。 (毕竟它没有真正声明。)

所以问题就变成了,如何调用最近动态创建的控件并使用该控件分配事件。如果这是不可能的,我不介意转储表格并重新开始创建 30 个秒表(但如果可能的话,我想避免这种情况)。

感谢您的任何帮助。

Below is some code that I'm using to create objects with Visual Basic:

    For indexCounter As Integer = 1 To TotalParticipants Step 1

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

        participantStop = New Button
        participantStop.Size = New Size(58, 20)
        participantStop.Location = New Point(245, (5 + ((indexCounter - 1) * 26)))
        participantStop.BackColor = Color.Red
        participantStop.ForeColor = Color.White
        participantStop.Font = New Font(participantStop.Font, FontStyle.Bold)
        participantStop.Text = "Stop"
        CenterPanel.Controls.Add(participantStop)

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

        participantStopwatch = New Stopwatch
        participantStopwatch.Start()
Next

I'm creating a label, a button, Timer, and Stopwatch. (Though I have sinking feeling I don't need BOTH a timer and stopwatch since I'm counting time.)

What I would like to do, is create the label and set that label's text to be the value from the stopwatch. The button that will be created will stop THAT stopwatch.

The problem that I'm having is that I cannot call the stopwatch by name since it wasn't created yet and VB throws a hissy fit at me for it. (After all it wasn't really declared.)

So the question becomes, how do you call the most recently dynamically created control and assign events using that control. If it's not possible to do, I do not mind dumping the form and starting over creating 30 stopwatches instead (but I'd like to avoid that, if possible).

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百思不得你姐 2025-01-09 01:19:58

我假设您希望计时器根据秒表的值更新标签。是这样吗?

您可能会尝试的一件事是有点hacky:
像这样定义一个存储类:

Public Class StopwatchStorage
    Public Property Stopwatch as Stopwatch
    Public Property Label as Label
    Public Property Timer as Timer
End Class

在表单的顶部定义一个私有列表:

Private _storage as new List(Of StopwatchStorage)

在 for 循环的末尾执行此操作

Dim storage As New StopwatchStorage()
storage.Label = participantClock
storage.Timer = participantTimer
storage.Stopwatch = participantStopwatch
_storage.Add(storage)
AddHandler participantTimer.Tick, AddressOf Timer_Tick

上面的代码将使您能够访问 tick 函数中所需的三个对象。您必须循环遍历 _storage 列表才能找到正确的对象“集”,但它应该可以工作:

Private Sub Timer_Tick(sender As Object, args As EventArgs)
    For Each storage As StopwatchStorage In _storage
        If storage.Timer Is sender Then
            storage.Label.Text = storage.Stopwatch.Elapsed
            Exit Sub
        End If
    Next
End Sub

我没有尝试编译该代码,所以我确信有一些拼写错误,但我认为这应该给您一个如何在不需要使用对象名称的情况下引用对象的想法。

I assume that you want the timer to update the label based on the value of the stopwatch. Is that right?

One thing that you might try that is a little hacky is this:
Define a storage class like so:

Public Class StopwatchStorage
    Public Property Stopwatch as Stopwatch
    Public Property Label as Label
    Public Property Timer as Timer
End Class

at the top of your form define a private list:

Private _storage as new List(Of StopwatchStorage)

at the end of your for loop do this

Dim storage As New StopwatchStorage()
storage.Label = participantClock
storage.Timer = participantTimer
storage.Stopwatch = participantStopwatch
_storage.Add(storage)
AddHandler participantTimer.Tick, AddressOf Timer_Tick

The above code would give you access to the three objects that you need in your tick function. You will have to loop through the _storage list to find the right "set" of objects but it should work:

Private Sub Timer_Tick(sender As Object, args As EventArgs)
    For Each storage As StopwatchStorage In _storage
        If storage.Timer Is sender Then
            storage.Label.Text = storage.Stopwatch.Elapsed
            Exit Sub
        End If
    Next
End Sub

I didn't try to compile that code so I'm sure there are a few typos but I think that should give you an idea of how to refer to the object without needing to use the object's name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文