PC Review


Reply
Thread Tools Rate Thread

Access to Controls created by code

 
 
Bernard Bourée
Guest
Posts: n/a
 
      21st Nov 2004
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


--
Bernard Bourée
(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      21st Nov 2004
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/de...classtopic.asp

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

I hope this helps?

Cor


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      21st Nov 2004
"Bernard Bourée" <(E-Mail Removed)> schrieb:
>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>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Event Code for Controls created at runtime... Robert Bruce Microsoft Excel Programming 0 6th Dec 2006 12:11 PM
Re: Event Code for Controls created at runtime... Bob Phillips Microsoft Excel Programming 0 6th Dec 2006 10:35 AM
Using events with controls created in code. Shawn Microsoft VB .NET 3 3rd Jun 2005 02:51 PM
Code not working ? After I created a Tab Control and Cut and Paste the controls to each tab. acs68 Microsoft Access Form Coding 3 27th Oct 2004 07:26 AM
Accessing dynamically created controls from code-behind Steve Black Microsoft ASP .NET 2 6th May 2004 05:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:14 PM.