Access to Controls created by code

B

Bernard Bourée

I have created various controls by code and added them to a form.

I need for some of them (TextBox) to modify the value.

I use the folowing code to obtain the TextBox wich name is given by sName.

But Me.Controls does not contains my TextBoxes but only the controls
implemented by hand.
This is also confirmed by the spy window.

Thanks for the help.
Dim ctl As Control

For Each ctl In Me.Controls

If TypeOf ctl Is TextBox Then

If ctl.Name = sName Then

Return ctl.Handle

End If

End If

Next
 
C

Cor Ligthert

Bernard,

Probably is it because you have added your control in another
controlcollection by instance a panel.

Than you can do using your own method

\\\
Dim ctl As Control
For Each ctl In Panel1.Controls
If TypeOf ctl Is TextBox Then
If ctl.Name = sName Then
Return ctl.Handle
End If
End If
Next
///

I advice you however when you dynamicly create controls to set the reference
of those in a kind of array, when you want to do it like this, that you need
to find them by name, than the hashtable can be very effective.

http://msdn.microsoft.com/library/d...frlrfsystemcollectionshashtableclasstopic.asp

However this is not the only solution, I use often simple arrays of
controls.

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

Bernard Bourée said:
I have created various controls by code and added them to a form.

I need for some of them (TextBox) to modify the value.

I use the folowing code to obtain the TextBox wich name is given by
sName.

But Me.Controls does not contains my TextBoxes but only the controls
implemented by hand.
This is also confirmed by the spy window.

If you added your controls to the form's 'Controls' collection, this
collection will contain the textboxes too. Make sure you set the controls'
'Name' property to the name so the comparison with the name will be
successful for the controls.

Additional information:

Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
 

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