473,511 Members | 16,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Directly referencing a control based on a string

I have a simple function which loops through all the controls on a form
searching for a specific one:

Public Function FindControl(ByVal ctrls As Control.ControlCollection,
ByVal ctrlName As String) As Control

For Each ctrl As Control In ctrls
If ctrl.Name = ctrlName.Trim Then
Return ctrl
End If
If ctrl.HasChildren Then
FindControl(ctrl.Controls, ctrlName)
End If
Next
Return Nothing
End Function

But how do I reference that control's properties simply by knowing it's
name? The results of the looping function above allows me to work with a
strongly typed object so I can directly reference the properties because it
finds the object given the name. But it is extremely inefficient. What I'm
trying to do is exactly what you can do with an ADO recordset where you
refer to a specific field by it's name:

myRecordset("txtFirstName") = "Robert"

I would like to do the same thing with the Controls collection of a given
form:

myForm.Controls("txtFirstName").Text = "Robert"

Of course this is not allowed in .Net but I'm trying to find the equivalent.
Basically, I need to refer to a control directly by using its name instead
of looping through all the controls to find one that matches the name I
already know.

I know it's a little confusing and I'm probably not explaining it very well,
but thanks for any assistance.

Thanks,
Tamarack
Nov 20 '05 #1
2 1544
"Tamarack" <bu*****@hotmail.com> schrieb
I have a simple function which loops through all the controls on a
form searching for a specific one:

Public Function FindControl(ByVal ctrls As
Control.ControlCollection,
ByVal ctrlName As String) As Control

For Each ctrl As Control In ctrls
If ctrl.Name = ctrlName.Trim Then
Return ctrl
End If
If ctrl.HasChildren Then
FindControl(ctrl.Controls, ctrlName)
End If
Next
Return Nothing
End Function

But how do I reference that control's properties simply by knowing
it's name? The results of the looping function above allows me to
work with a strongly typed object so I can directly reference the
properties because it finds the object given the name. But it is
extremely inefficient. What I'm trying to do is exactly what you can
do with an ADO recordset where you refer to a specific field by it's
name:

myRecordset("txtFirstName") = "Robert"

I would like to do the same thing with the Controls collection of a
given form:

myForm.Controls("txtFirstName").Text = "Robert"

Of course this is not allowed in .Net but I'm trying to find the
equivalent. Basically, I need to refer to a control directly by using
its name instead of looping through all the controls to find one that
matches the name I already know.

I know it's a little confusing and I'm probably not explaining it
very well, but thanks for any assistance.


You explained it very well.

First, /why/ do you need the name? Names are used at design time as
expressive names for the programmer and are resolved at compile time but
shouldn't matter at runtime. The compiler can not find misspelled names
placed in Strings. You'd get a runtime error.

Despite: you could use a modified version of the loop above and add each
found control to a Hashtable. The items in the Hashtable (= the controls)
can be accessed by their names. The loop can run once.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
"Tamarack" <bu*****@hotmail.com> schrieb
I have a simple function which loops through all the controls on a
form searching for a specific one:

Public Function FindControl(ByVal ctrls As
Control.ControlCollection,
ByVal ctrlName As String) As Control

For Each ctrl As Control In ctrls
If ctrl.Name = ctrlName.Trim Then
Return ctrl
End If
If ctrl.HasChildren Then
FindControl(ctrl.Controls, ctrlName)
End If
Next
Return Nothing
End Function

But how do I reference that control's properties simply by knowing
it's name? The results of the looping function above allows me to
work with a strongly typed object so I can directly reference the
properties because it finds the object given the name. But it is
extremely inefficient. What I'm trying to do is exactly what you can
do with an ADO recordset where you refer to a specific field by it's
name:

myRecordset("txtFirstName") = "Robert"

I would like to do the same thing with the Controls collection of a
given form:

myForm.Controls("txtFirstName").Text = "Robert"

Of course this is not allowed in .Net but I'm trying to find the
equivalent. Basically, I need to refer to a control directly by using
its name instead of looping through all the controls to find one that
matches the name I already know.

I know it's a little confusing and I'm probably not explaining it
very well, but thanks for any assistance.


You explained it very well.

First, /why/ do you need the name? Names are used at design time as
expressive names for the programmer and are resolved at compile time but
shouldn't matter at runtime. The compiler can not find misspelled names
placed in Strings. You'd get a runtime error.

Despite: you could use a modified version of the loop above and add each
found control to a Hashtable. The items in the Hashtable (= the controls)
can be accessed by their names. The loop can run once.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1560
by: DC Gringo | last post by:
I have an myPage.aspx with a myPage.ascx user control. The user control is the main header of the page. I have some conditional case code in the user control that I use which depends on a public...
0
1245
by: Alex VanderWoude | last post by:
Okay, now I know how to directly reference a particular overload of a method in a cref. However, now I have the "opposite" problem. I want to directly reference the general summary page for all...
2
236
by: Tamarack | last post by:
I have a simple function which loops through all the controls on a form searching for a specific one: Public Function FindControl(ByVal ctrls As Control.ControlCollection, ByVal ctrlName As...
9
7352
by: Alan | last post by:
Hmmm, I'm not too good with the syntax of referencing a subreport. I have frmInvoice which has the invoice details (e.g. ProductCode, ProductCost etc) in the subform frmInvoiceDetails. I'm trying...
0
1092
by: Chris | last post by:
I want to display a formview based on an objectdatasource. One of the things on the formview will be a link to a pdf. When I go into the edit template I would like a fileupload to display, which...
21
5066
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main...
4
1885
by: adam_kroger | last post by:
BRIEF EXPLANATION: I have 6 TextBoxes named LIS1, LIS2, LIS3, ... LIS6. I want to be able to reference them using a For/Next loop and either read ot write to them. In VBA I would use something...
5
1365
kcdoell
by: kcdoell | last post by:
Good Morning: I am using the following code to add new records to a table that is on my Sub Form. Below is the code that I placed in my Before Update event on my Sub Form: Private Sub...
3
1875
topher23
by: topher23 | last post by:
I'm trying to use a WebBrowser control to automate data entry from our local Access system to the web-based corporate ERP system. Unfortunately, I've run into a snag. Several of the text boxes that...
0
7138
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
7355
Oralloy
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,...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
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...
1
5066
isladogs
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...
0
3225
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...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
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 ...
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.