Connecting Tech Pros Worldwide Help | Site Map

get Control by Name

  #1  
Old November 17th, 2005, 02:25 AM
Sharon
Guest
 
Posts: n/a
Hi all

Is it possible to refer a control by his name programmatically?



Example:



I have text box and a button on a form

txtName

btnClick





And what I want to do is:



myForm.GetControl("txtName").text = "xxx"

myForm.GetControl("btnClick").text = "xxx"


  #2  
Old November 17th, 2005, 02:25 AM
Maciej
Guest
 
Posts: n/a

re: get Control by Name



Użytkownik "Sharon" <Sharon669@hotmail.com> napisał w wiadomości
news:u#NF6eBUFHA.2520@TK2MSFTNGP09.phx.gbl...[color=blue]
> Hi all
>
> Is it possible to refer a control by his name programmatically?
>
>
>
> Example:
>
>
>
> I have text box and a button on a form
>
> txtName
>
> btnClick
>
>
>
>
>
> And what I want to do is:
>
>
>
> myForm.GetControl("txtName").text = "xxx"
>
> myForm.GetControl("btnClick").text = "xxx"
>[/color]

You should use:
this.txtName.text="xxx";
and
this.btnClick.text="xxx";

  #3  
Old November 17th, 2005, 02:25 AM
Lebesgue
Guest
 
Posts: n/a

re: get Control by Name


> Is it possible to refer a control by his name programmatically?
Yes, it is possible if you loop through the list of your controls and
compare their names with the name you are refering to.

private Control FindControlByName(string name)
{
foreach (Control c in this.Controls) //assuming this is a Form
{
if (c.Name == name)
return c; //found
}
return null; //not found
}


  #4  
Old November 17th, 2005, 02:26 AM
Sharon
Guest
 
Posts: n/a

re: get Control by Name


Well I thought that there is an option to go directly to the control by its
name,

This loop will increase the processing & presenting time since there are
lots of other controls on this form



By the way In JS there is a method

ˇ getObjectByName: Method returns a registered object based on the
specified name.



Thank you anyway

Sharon





"Lebesgue" <nospam@spam.jp> wrote in message
news:Olwyt3BUFHA.3944@tk2msftngp13.phx.gbl...[color=blue][color=green]
>> Is it possible to refer a control by his name programmatically?[/color]
> Yes, it is possible if you loop through the list of your controls and
> compare their names with the name you are refering to.
>
> private Control FindControlByName(string name)
> {
> foreach (Control c in this.Controls) //assuming this is a Form
> {
> if (c.Name == name)
> return c; //found
> }
> return null; //not found
> }
>
>[/color]


  #5  
Old November 17th, 2005, 02:28 AM
Lebesgue
Guest
 
Posts: n/a

re: get Control by Name


> Well I thought that there is an option to go directly to the control by
its[color=blue]
> name,
>
> This loop will increase the processing & presenting time since there are
> lots of other controls on this form[/color]

If you have many controls on the form, you can use for example Hashtable and
add all your Controls to it with their name as a key. Accessing items in a
hashtable is a constant time operation, so there is virtually no performance
hit if you use the hashtable.

On form.Load or elsewhere

this.controlHashtable = new HashTable();
foreach Control c in this.Controls
{
this.controlHashtable.Add(c.Name, c);
}

private Control GetControlByName(string name)
{
return this.controlHashtable[name] as Control;
}


  #6  
Old November 17th, 2005, 02:28 AM
Sharon
Guest
 
Posts: n/a

re: get Control by Name


Great idea
thank you !!!!


"Lebesgue" <nospam@spam.jp> wrote in message
news:eAJcs8LUFHA.3448@TK2MSFTNGP10.phx.gbl...[color=blue][color=green]
>> Well I thought that there is an option to go directly to the control by[/color]
> its[color=green]
>> name,
>>
>> This loop will increase the processing & presenting time since there are
>> lots of other controls on this form[/color]
>
> If you have many controls on the form, you can use for example Hashtable
> and
> add all your Controls to it with their name as a key. Accessing items in a
> hashtable is a constant time operation, so there is virtually no
> performance
> hit if you use the hashtable.
>
> On form.Load or elsewhere
>
> this.controlHashtable = new HashTable();
> foreach Control c in this.Controls
> {
> this.controlHashtable.Add(c.Name, c);
> }
>
> private Control GetControlByName(string name)
> {
> return this.controlHashtable[name] as Control;
> }
>
>[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to search some control by name? aps-asia answers 1 March 22nd, 2006 03:35 PM
Refering to a Control by Name In Code D Miller answers 2 December 27th, 2005 10:05 PM
Accessing a control by name Jack Russell answers 10 November 23rd, 2005 04:36 AM
Reference dynamically created control by name mef526 answers 7 November 21st, 2005 06:50 AM