tags:

views:

1070

answers:

2

i have a System.Windows.Forms.Panel control in a windows application that has many labels and textboxes within the panel. When you call the Panel.Enabled = false command all of the textboxes and labels go gray and become un-editable which is the desired effect. Is there a way to overload the light gray colors when the panel is disabled?

Solution used:

Since the disabled color cant be overiden when disabled the following was used to disable only text boxes and labels on the Panel rather than the blanked Panel.Enabled = false;.

//Loop through the Member info Panel and find all the group boxes 
foreach (Control cItem in Panel.Controls)
{
    //A group box is found 
    if (cItem is GroupBox)
    {
     //Loop through all the group box components
     foreach (Control cSubItem in cItem.Controls)
     {
      //If its a label of text box disable it
      if (cSubItem is TextBox || cSubItem is Label) 
      { 
       cSubItem.Enabled = false; 
      }
     }
    }
}
+3  A: 

There is no way to do a blanket override of how each Label and TextBox are Colored when they are disabled. But you can override each individual control after the containing panel is disabled and set them to the appropriate color.

The TextBox class (and more specifically the TextBoxBase class) will prefer an explicit color over the default Gray color for painting when disabled.

I wrote a blog post awhile back on how to achieve this effect: http://blogs.msdn.com/jaredpar/archive/2007/02/12/readonly-textbox-that-doesn-t-look-funny.aspx

JaredPar
+1  A: 

As an alternative: If the controls you wish to have a different enabled/disabled color you could iterate through the controls registered with the panel and assign their EnabledChanged event a custom function to change their color (based on type I assume). You'd probably want to filter the type of control you modify with this but you could use this to achieve your goal I believe.

Update: Try this: public void deselected(object sender, EventArgs e) { foreach (Control c in this.Controls) { //TODO:check type of control and change background color } } That will need to be placed inside your Form class but then you can use it on a event. Hope that helps.

I have been looking for logic to iterate on. The panel dose not seam to have a child collection so i was going to iterate on the form level which will work but need to figure out what the main parent node is.
Ioxp