Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change a property for all controls on a form?

Status
Not open for further replies.

ddvlad

Programmer
Mar 12, 2003
71
RO
I've been trying to change the FlatStyle for all Controls that support it in my form. However, I have multiple containers (panels) in it and code such as this just won't work:

foreach (object o in this.Controls)
if (o is ButtonBase)
(o as ButtonBase).FlatStyle = FlatStyle.Flat;

I thought a recursive method might help, but it somehow doesn't. I tried to look for another collection and check its controls as well, but it still doesn't work. Any sugestions?

Thanks.

There is nothing more dangerous than an idea when it is the only one you have.
 
a recursive method does work
Code:
public void changeButtonBase(Control ctrl)
{
     if (ctrl.HasControls)
     {
          foreach (Control ct in ctrl.Controls)
          {
               if (ct is ButtonBase) ((ButtonBase)ct).FlatStyle=FlatStyle.Flat;
               changeButtonBase(ct);
          }
     }
}

and you would call it like
changeButtonBase(this);

(didn't actually write it now in VS but it should work)

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Thanks for the sugestion. I hope it works.

There is nothing more dangerous than an idea when it is the only one you have.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top