dynamically createing controls

B

Brian Henry

How would you dynamically create controls and give each one a unique name? I
know how to make one control and add it to the forms controls collection x
number of times, but what if i want to change the name for each one so its
like control1,control2,control3, etc etc... how would you do that? I was
thinking a string built with the incrementing id but how do you turn that
string into a control? directcast/ctype? thanks!
 
W

William Ryan eMVP

Will this work:

For i As Integer = 0 To 3

Dim s As String = "TextBox" & i.ToString

Dim c As New TextBox

c.Name = s

MessageBox.Show(c.Name)

Next
 
H

Herfried K. Wagner [MVP]

* "Brian Henry said:
How would you dynamically create controls and give each one a unique name? I
know how to make one control and add it to the forms controls collection x
number of times, but what if i want to change the name for each one so its
like control1,control2,control3, etc etc... how would you do that? I was
thinking a string built with the incrementing id but how do you turn that
string into a control? directcast/ctype? thanks!

Add all controls to a 'Hashtable' and use the controls' names as keys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top