We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,970 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to set consecutive control names in FOR statement?

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
2
Contributors
7
Replies
1 Day
Discussion Span
3 Months Ago
Last Updated
8
Views
Question
Answered
boher
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

Reverend Jim
Carpe per diem
Moderator
3,596 posts since Aug 2010
Reputation Points: 561
Solved Threads: 446
Skill Endorsements: 32

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.

boher
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.

Reverend Jim
Carpe per diem
Moderator
3,596 posts since Aug 2010
Reputation Points: 561
Solved Threads: 446
Skill Endorsements: 32

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.

boher
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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?

Reverend Jim
Carpe per diem
Moderator
3,596 posts since Aug 2010
Reputation Points: 561
Solved Threads: 446
Skill Endorsements: 32

thats awesome, thanks

boher
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

No problem. Glad we got it straightened out.

Reverend Jim
Carpe per diem
Moderator
3,596 posts since Aug 2010
Reputation Points: 561
Solved Threads: 446
Skill Endorsements: 32
Question Answered as of 3 Months Ago by Reverend Jim

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0960 seconds using 2.67MB