Hi,

For 2 days i have tried to access a button, by a string value. In javascript this would be very simple, but i can't seem to get it done with c#. Can someone please tell how me to do the following?

string buttonname;
buttonname = "btnAdd";

buttonname.Enabled = true;

Thanks in advantage!

Your code will give a compile error saying that string does not have a field called Enabled.
This is what you can do:

Button mybutton = new Button(); //create button
myButton.Name = "whatever"; //give it a name
myButton.Enable = true; //enable it

Hmm, this is not really what i mean.

I have a lot of buttons on my form. which buttons are enabled depands on my user level.

all buttons names are in a database. My query retrieves all the buttons that should be enabled. But now i need to set these buttons enabled. How can i do this?

Use Controls collection.

String btn="button1";
  this.Controls[btn].Enabled = false;

Thanks a lot! this works, but now i have to put the control in the place of this.

like:

string btn = "btnToevoegen";
            ((Button)ACFzoekenGroupBox.Controls[btn]).Enabled = true;

when i use this. instead of ACFzoekenGroupBox. is does not work.
Is it possible to use something else then ACFzoekenGroupBox. so it will be able to change all the controls on the form?

string p = "button4";
   this.Controls["groupBox1"].Controls[p].Enabled = false;

Now i can only access the buttons on groupBox1... is there a way i can access all buttons in a form, including all the buttons in all groupboxes that are on the same form?

Yes you can.

Mark this thread as Solved if get solution.

oke, thanks for all your help!

Every form has a Controls collection, as adatapost showed you.

use:

this.Controls.Find("buttonName", true)

It will search for the named control in child containers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.