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 6 14678
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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |