I need a solution for a small problem in my project and I'll make it as simple as possible:

At one of the forms in my project, user should be able to add a panel to the form and that form should contain 2 controls ... a label and a (stopwatch or timer).

The problem is after creating the stopwatch dynamically, I can't set a name for the stopwatch. Later in that form when user clicks on the panel stopwatch must start. How can I set the name for created stopwatch? or any other solution if my way is too stupid. Here's what I have :

Dim Panels As Integer = ds.Tables("Pool").Rows.Count
        For i = 1 To Panels 
            'set the new panel number
            Dim NextPanelNumber As Integer = FlowLayoutPanel1.Controls.Count + 1
            'controls to be added
            Dim NextPanel As New PanelControl
            Dim NextPanelLabel As New LabelControl
            Dim NextPanelStopwatch As New Stopwatch
            'determine properties for new table
            NextPanel.Width = 200
            NextPanel.Height = 114
            NextPanelStopwatch.Name = "anything" ' <-- PROBLEM IS HERE

            'add controls into new panel

The name is NextPanelStopwatch. To start it you do


Just make sure you declare it where it won't go out of scope.

or any other solution if my way is too stupid

I would not say its stupid, just not well thought out. :)

Look at what you wrote as one of your goals: " Later in that form when user clicks on the panel stopwatch must start".

To accomplish this, you will be handling the panel's "Click" event. That has nothing to do with the name that you would assign to the Stopwatch ****if it had a Name property. ****

What you do need however is a reference to the StopWatch so that you can start and stop it. All Controls have a "Tag" Property, so you could assign the StopWatch to the Panel's Tag.

However this is just plain messy and hard to maintain. I suggest that you consider creating a UserControl instead. It would house it's own timer and Click logic. Then you would just add an instance of that instead of the panel to your form.

TnTinMN I see what you mean but I think I didn't explain the code correctly. As you can see the 'FOR' statement counts the number of panels in the flowlayoutpanel and must create a stopwatch for each panel and name them after the panel number for ex. if there are 3 panels (panel1, panel2, panel3) the code must create 3 stopwatches with names : stopwatch1, stopwatch2 and stopwatch3. How to dod that ?

The Stopwatch control gets its name from the dim statement right where you state it. that's what I know at least. I mean like :

Dim Stopwatch1 as Stopwatch

The name of that stopwatch would be "Stopwatch1".

Technically that not its name. That's just a reference to it. Consider

Dim sw1 As New StopWatch
Dim sw2 As StopWatch = sw1

sw1 and sw2 are both references to the same object but neither is the object's name. For most controls, Name is an actual property of the object (intrinsic). sw1 and sw2 exist separately from the object (extrinsic). When you destroy the object, sw1 and sw2 continue to exist.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.