Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 17th, 2005, 02:25 AM
Sharon
Guest
 
Posts: n/a
Default get Control by Name

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
Default 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
Default 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
Default 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
Default 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
Default 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]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.