accessing control by its id in variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i have textboxes with ids tb1,tb2,tb3...
now i want to set text to these textboxes somehow like

for (i=1;i<5;i++)
("tb"+i).Text = i;

obviously it won't work.
How to translate ("tb"+i) into actual control id?
is there anyway to access properties of controls using ids stored in
variables ?

please help
thanks a lot
 
ashim,
Yes. You can store all the controls in a Hashtable at startup and access
them from there by key.

Otherwise, what you what to do is more like
foreach (Control c in this.Controls0
{
if (c.id.Substring(0,2)=="tb')
// your cool stuff here
}

.... etc

Peter
 
Use the controls collection member of the form or reflection to implement
what you need
 
Thanks Peter. I was thinking,
without looping through all the controls, if we could have something like
getControl(id) to get the control's reference directly.
 
ashim said:
Thanks Peter. I was thinking,
without looping through all the controls, if we could have something like
getControl(id) to get the control's reference directly.


:


for (i=1;i<5;i++)
owningControl.Controls["tb"+i].Text = i;

HTH,
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top