1,105,456 Community Members

How to set consecutive control names in FOR statement?

Member Avatar
boher
Newbie Poster
14 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
Reverend Jim
Noli mentula
5,445 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

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

Member Avatar
boher
Newbie Poster
14 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Reverend Jim
Noli mentula
5,445 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
boher
Newbie Poster
14 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Reverend Jim
Noli mentula
5,445 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

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?

Member Avatar
boher
Newbie Poster
14 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

thats awesome, thanks

Member Avatar
Reverend Jim
Noli mentula
5,445 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

No problem. Glad we got it straightened out.

Question Answered as of 1 Year Ago by Reverend Jim
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: