Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7405995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:29:18+00:00 2026-05-29T05:29:18+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:29:19+00:00Added an answer on May 29, 2026 at 5:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below I am posting some code that I am using to try to set
I have some code that looks like the below. Does this create a deadlock?
Below is some code I've written that is effective, but makes too many database
below is some code I am using to translate a map array into SQL
I have some code that works like the below. I was wondering if its
Let us say we have some code that looks like below: @interface SomeClass :
I have some code that is using mechanize and a password protected site. I
I have some code below that is accessing the registry. The application is built
I have some code that performs a deep copy using Object.clone, but I'm trying
I have some code that works and changes the style sheet using a form.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.