By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,032 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,032 IT Pros & Developers. It's quick & easy.

getting controls by name

P: n/a
In VB 6, I believe if you had a control named MyControl on a form named
MyForm, you could obtain a reference to MyControl with a string as in
MyForm.Controls("MyControl"). Is there an equivalent in .NET??

Thanks,
Christine

Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hello,

"Christine Nguyen" <hc********@nospam.hotmail.com> schrieb:
In VB 6, I believe if you had a control named MyControl on
a form named MyForm, you could obtain a reference to
MyControl with a string as in MyForm.Controls("MyControl").
Is there an equivalent in .NET??


\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("btnBla", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to access a
lot of controls by name very often, you should store references to them in a
'Hashtable' object. You can use the name of the control as key.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #2

P: n/a
Hi Herfried,

Yes I am currently using a Hashtable. I was wondering, however, if there
were any direct implementations built into the framework, but I guess not

Thanks,
Christine
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:ef*************@tk2msftngp13.phx.gbl...
Hello,

"Christine Nguyen" <hc********@nospam.hotmail.com> schrieb:
In VB 6, I believe if you had a control named MyControl on
a form named MyForm, you could obtain a reference to
MyControl with a string as in MyForm.Controls("MyControl").
Is there an equivalent in .NET??
\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("btnBla", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to access a
lot of controls by name very often, you should store references to them in

a 'Hashtable' object. You can use the name of the control as key.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet

Nov 20 '05 #3

P: n/a
Fergus, thanks for the additional input.

-Christine

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:#z**************@TK2MSFTNGP11.phx.gbl...
Hi Christine,

Your guess is correct. Gone, too, is the collection of Forms. :-(

Your use of a Hashtable is a good idea but one which requires you to
maintain it, ie, populate it and ensure that it is up to date (if you add or delete controls). In your next program you'll have to do the same, and so on.
Herfried's recursive FindControl will find the given Control without
needing you to have done anything to set it up.You might be concerned about performance, however, if you expect to do a lot of looking up.

A very useful and totally reusable method (with near-zero setup costs for the developer using it) would be a class that contains <both> your solutions. The FindControl entry point would search the Hashtable, if not found
Herfried's recursion would find it, then it would be added to the Hashtable as well as being returned.

Regards,
Fergus

Nov 20 '05 #4

P: n/a
Christine,
In addition to Fergus's comments, you could handle the ControlAdded &
ControlRemoved events, then based on these events update the hashtable. If
you have a lot of forms that need this, you could define a new base form
that all your forms inherit from.

I have not used these events, my concern would be they are not raised when
controls are added to control containers such as Group Boxes. You would need
to handle their ControlAdded & ControlRemoved events. With AddHandler &
RemoveHandler this would not be that hard, as you could actually use the
same event handlers in the form to handle all control containers...

Hope this helps
Jay

"Christine Nguyen" <hc********@nospam.hotmail.com> wrote in message
news:e0*************@tk2msftngp13.phx.gbl...
Hi Herfried,

Yes I am currently using a Hashtable. I was wondering, however, if there
were any direct implementations built into the framework, but I guess not

Thanks,
Christine
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:ef*************@tk2msftngp13.phx.gbl...
Hello,

"Christine Nguyen" <hc********@nospam.hotmail.com> schrieb:
In VB 6, I believe if you had a control named MyControl on
a form named MyForm, you could obtain a reference to
MyControl with a string as in MyForm.Controls("MyControl").
Is there an equivalent in .NET??
\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("btnBla", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to access a lot of controls by name very often, you should store references to them

in a
'Hashtable' object. You can use the name of the control as key.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet


Nov 20 '05 #5

P: n/a
That's a great thought, Jay! Definitely something to consider.

Thanks,
-Christine

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:#p*************@TK2MSFTNGP11.phx.gbl...
Christine,
In addition to Fergus's comments, you could handle the ControlAdded &
ControlRemoved events, then based on these events update the hashtable. If
you have a lot of forms that need this, you could define a new base form
that all your forms inherit from.

I have not used these events, my concern would be they are not raised when
controls are added to control containers such as Group Boxes. You would need to handle their ControlAdded & ControlRemoved events. With AddHandler &
RemoveHandler this would not be that hard, as you could actually use the
same event handlers in the form to handle all control containers...

Hope this helps
Jay

"Christine Nguyen" <hc********@nospam.hotmail.com> wrote in message
news:e0*************@tk2msftngp13.phx.gbl...
Hi Herfried,

Yes I am currently using a Hashtable. I was wondering, however, if there
were any direct implementations built into the framework, but I guess not

Thanks,
Christine
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:ef*************@tk2msftngp13.phx.gbl...
Hello,

"Christine Nguyen" <hc********@nospam.hotmail.com> schrieb:
> In VB 6, I believe if you had a control named MyControl on
> a form named MyForm, you could obtain a reference to
> MyControl with a string as in MyForm.Controls("MyControl").
> Is there an equivalent in .NET??

\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("btnBla", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to
access a lot of controls by name very often, you should store references to
them in
a
'Hashtable' object. You can use the name of the control as key.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet



Nov 20 '05 #6

P: n/a
Hello,

"Christine Nguyen" <hc********@nospam.hotmail.com> schrieb:
Yes I am currently using a Hashtable. I was wondering,
however, if there were any direct implementations built
into the framework, but I guess not


No, not for Windows Forms.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.