Hi I need a code block to accomplish the following task :

There is a flolayoutpanel in my form with 3 panels in it. I want my submit button to count the panels and create a stopwatch control named after each panel. Here is what I have so far :

Dim Panels As Integer = FlowLayoutPanel1.Controls.Count
        For i = 1 To Panels 
            Dim StopwatchNumber As Integer = FlowLayoutPanel1.Controls.Count + 1
            Dim Stopwatch As New Stopwatch ' <-- How can I assign the name [Stopwatch1]?
        Next

The StopWatch object does not have a name property. Please do not post duplicate threads.

Ok you need to pay attention to my question, this is not a duplicate thread. This is not about my prior question about Stopwatch name property. This is about naming a control in runtime.

Other thread

Dim NextPanelStopwatch As New Stopwatch

This thread

Dim Stopwatch As New Stopwatch ' <-- How can I assign the name [Stopwatch1]?

Both threads create a StopWatch object at runtime and in the last thread I explained that there is no Name property of the StopWatch object. How is that not a duplicate thread? You need to pay attention to the answer or state the question more clearly. You said How can I assign the name [Stopwatch1]?. What do you want to assign that name to?

If I am missing something obvious here I apologize but it's 02:25 AM and I'm more than a little tired from not sleeping.

Assume in this thread,

Dim NextPanel as New Panel
Nexpanel.name = "Panel" & 1 & 2 & ... & i

At least I know Panel has a name property.
Was that clear enough? I want to assign consecutive names to controls while creating them.

That's clearer. I thought you were still trying to assign a name to the StopWatch object. If you are creating all of the panels at the same time (ie in a loop inside a code block, then you can just use a local variable. If you are doing the creation at different times (ie different calls to a sub or function) then you will have to make the variable global. For the local case you get

for i = 1 to 5
    dim NextPanel as New Panel
    NextPanel.Name = "Panel" & i.Tostring()
    Me.Controls.Add(NextPanel)
next

for the global case you get

Public Form Form1

    Private panelnum As Integer = 0
    .
    .
    .

    Private Sub MySub()

        for i = 1 to 5
            panelnum += 1
            dim NextPanel as New Panel
            NextPanel.Name = "Panel" & panelnum.Tostring()
            Me.Controls.Add(NextPanel)
        next

Is that more what you had in mind?

thats awesome, thanks

No problem. Glad we got it straightened out.

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.