473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

getting controls by name

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
6 14669
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sibyl | last post by:
Is there any way to get the name of a class without an instance (i.e., object of the class)? I am working with log4j, and would like a uniform way to name loggers without typing in the name of the...
2
by: Gerry Abbott | last post by:
Hi all. Im using abbreviated field names on a form to optimise space, but would like to display for example, the status bar text of the control, onto the form header, when the control has the...
1
by: msnews.microsoft.com | last post by:
I'm adding multiple web controls to the placeholder controls collection, but even when doing the for each and recursion method of getting the web controls, I'm only getting the first web control...
3
by: Patrick | last post by:
I am dynamically creating TextArea and drop-down lists in ASP.NET using something like HtmlTextArea eachTextArea = new HtmlTextArea(); I tried to set the "name" of these TextAreas, etc. (e.g....
17
by: Mitko Haralanov | last post by:
I need to be able to get the name of the currently executed method within that method. I know that the method object does have the __name__ attribute but I know know how to access it from withing...
6
by: Adam Atlas | last post by:
Is it possible for an object, in its __init__ method, to find out if it is being assigned to a variable, and if so, what that variable's name is? I can think of some potentially ugly ways of...
2
Auz
by: Auz | last post by:
Hello. I have problem with getting the name of a function with javascript. The code I have works fine in firefox, but not in internet explorer. Its part of a much larger whole, but what it boils...
2
by: Heike Pertzel | last post by:
Hallo NG! I would like to get the name of the user logged on on a another computer. I've written the following code which gives me the LogonId of this user. But getting with this Id the name of...
1
by: Jim Hoeffer | last post by:
I am trying to get a label control on a form to display a control's name (i.e., field name from a table) when the control is selected. I know I can program each control's GotFocus event to do this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.