473,503 Members | 2,136 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 1543
"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
1090
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
1884
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
1361
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
7204
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
7342
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...
1
6998
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
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
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
4680
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...
0
3171
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
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.