dynamically generated object names

  • Thread starter Thread starter dinobuiatti
  • Start date Start date
D

dinobuiatti

Guest
Let's say I had 5 text boxes named text1, text2, text3......text5. Assuming I wanted to set their values to 1,2,3,4, and 5, I could do the following:

text1.text = "1"
text2.text = "2"
text3.text = "3"
text4.text = "4"
text5.text = "5"

But a better way would be to do it dynamically. Something like:

' declare variables
dim countervalue, textfieldname

' clear variable
countervalue = 1

' for loop
for countervalue = 1 to 5

' create reference to text field
textfieldname = text & countervalue

' set text value for current text field
textfieldname.text = countervalue

Next

Of course in my example, the the text1.text is evaluated as a text instead of a reference to the text field

How can I build a dynamic structure that refers to text fields and other controls.

Thnx
Dino B
 
I really am not sure if this is what your looking for, but if you create a control array, you can dynamically create new controls. An example of textboxes would be ...

Visual Basic:
Sub cmdCreateNewTextBox_Click()
    Static intCount As Integer
    'Get the next array index number (new controls index)
    intCount = intCount + 1
    'Create the new control dynamically
    Load txtInput(intCount)
    'Set the position for upper-left corner of new control so
    'it won't fall on top of previous dynamically created
    'controls
    txtInput(intCount).Top = txtInput(intCount - 1).Top + 495
    txtInput(intCount).Left = txtInput(intCount - 1).Left
    
   'Insert a value as you did in your example
   txtInput(intCount).text = intCount

    'Now, display the new control
    txtInput(intCount).Visible = True
End Sub

You could dynamically unload the textboxes using ...

Visual Basic:
Sub cmdUnloadControl_Click
    Unload txtInput(3) 'or a loop, depending on what you want.
End Sub
 
Let me simplify.

lets say i have one textbox.

textbox1

when i do the following from a submit button, it wont compile

dim intergervalue
intervalue = 1
textbox(intervalue).text = "value"

how would i do this

thank you

dinob
 
Let me simplify too. Use a control array - like Cogen said - then you can reference an array of controls via an Index property. Alternatively, create an array of generic Control object variables, then fill it with references to any controls on your form that you want to use in your routines.
 
Control arrays are perfectly possible in .NET, they're just not supported by the designers. Look for this in newer versions.
 
Back
Top