PC Review


Reply
Thread Tools Rate Thread

Directly referencing a control based on a string

 
 
Tamarack
Guest
Posts: n/a
 
      11th Apr 2004
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


 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      11th Apr 2004
"Tamarack" <(E-Mail Removed)> 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

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Directly referencing multiple cells JE McGimpsey Microsoft Excel New Users 4 11th Apr 2007 05:40 PM
XML string directly to MSAccess table records?? Don Microsoft VB .NET 0 12th Feb 2006 09:39 PM
referencing control value in RowSource SQL string =?Utf-8?B?UmFuZGFsbCBBcm5vbGQ=?= Microsoft Access VBA Modules 5 13th Sep 2005 10:49 PM
accessing string buffer posessed by System::String class directly? Bae,Hyun-jik Microsoft VC .NET 2 25th Aug 2005 06:47 AM
Send a string directly to a file Greg Chu Microsoft VB .NET 3 23rd Nov 2004 08:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:11 AM.