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?

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?

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?

use:

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

It will search for the named control in child containers.

This article has been dead for over six months. Start a new discussion instead.