Connecting Tech Pros Worldwide Help | Site Map

get Control by Name

Sharon
Guest
 
Posts: n/a
#1: Nov 17 '05
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"


Maciej
Guest
 
Posts: n/a
#2: Nov 17 '05

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";

Lebesgue
Guest
 
Posts: n/a
#3: Nov 17 '05

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
}


Sharon
Guest
 
Posts: n/a
#4: Nov 17 '05

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]


Lebesgue
Guest
 
Posts: n/a
#5: Nov 17 '05

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;
}


Sharon
Guest
 
Posts: n/a
#6: Nov 17 '05

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