access control by string

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

Guest

Hi all,

I would like to access just one of several controls by its number:

I do not want call:
Button1.Text

instead I want to call:
Button+"1".Text

"1" will be a char or byte or something else and come from another function.

I solved it in Delphi years ago - what about C#?
Can anybody help?
 
Its actually not that difficult, all you have to do do is access the
ControlCollection that contains your buttons. So in the case of a Windows
Form there is a property called Controls (its a ControlCollection) and you
can access members by index (int) or key (string).

So for a quick example I created a very simple WinForm app and placed a
couple buttons on it and placed the following in the form load event
handler:

this.Controls["button1"].Text = "Test";

and sure enough the button text changed from button1 to Test on load. You
will want to cast your control to a button if you want to access button
specific properties/methods. The only reason my example works without a cast
is because Text is inherited from the Control class.

Hope that helps.

-Matt Newman
-http://www.bestsnowman.com/
 
Hi

Also it might be useful to use

if (this.Controls.ContainsKey("button1"))
{
...
}

to make sure that the button exists

hope that helps too

T F

Matthew Newman said:
Its actually not that difficult, all you have to do do is access the
ControlCollection that contains your buttons. So in the case of a Windows
Form there is a property called Controls (its a ControlCollection) and you
can access members by index (int) or key (string).

So for a quick example I created a very simple WinForm app and placed a
couple buttons on it and placed the following in the form load event
handler:

this.Controls["button1"].Text = "Test";

and sure enough the button text changed from button1 to Test on load. You
will want to cast your control to a button if you want to access button
specific properties/methods. The only reason my example works without a
cast
is because Text is inherited from the Control class.

Hope that helps.

-Matt Newman
-http://www.bestsnowman.com/

Rob said:
Hi all,

I would like to access just one of several controls by its number:

I do not want call:
Button1.Text

instead I want to call:
Button+"1".Text

"1" will be a char or byte or something else and come from another
function.

I solved it in Delphi years ago - what about C#?
Can anybody help?
 
Thanks for fast reply, but my problem is still there...

My Button is a custom control and I cannot access for example:

this.Controls["bmcButton1"].Image

Looks difficult...
 
This would work, but I don't want to walk through all controls.

Just:

public void InterpreteRXCOM1(byte c)
{

Button+c.ToString().Image = ...

}

Rob
 
What you are asking for is the equivalent of the Javascript "eval" Statement.
It doesn't exist in C#.
Peter
 

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