Jump to content
Xtreme .Net Talk

dynamically generated object names


Recommended Posts

Guest dinobuiatti
Posted

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

Posted

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 ...

 

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 ...

 

Sub cmdUnloadControl_Click
   Unload txtInput(3) 'or a loop, depending on what you want.
End Sub

Guest dinobuiatti
Posted

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

Posted
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.
Guest dinobuiatti
Posted
Control arrays are not supported in .NET

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...