简体   繁体   中英

C# TableLayoutPanel.BackColor keeps resetting

When using the System.Windows.Forms.TableLayoutPanel control on a windows form, it sets the background color too: 216, 216, 216 , and even if we change the color to something else, the color always seems to reset back to 216, 216, 216 .

It does it at run time, and even during design time. During design time, you can change the color, and it resepct the color change, but if you switch away from the form designer, and back to it, it resets back to 216, 216, 216 .

Is this a known issue, or how can we avoid this issue?

Note : We did do this using just a blank project, single table layout using .NET Framework 4 (not client framework).

We are using

  • Microsoft Visual Studio 2010
  • Version 10.0.30319.1 RTMRel
  • Microsoft .NET Framework Version 4.0.30319 RTMRel
  • Installed Version: Ultimate

C# Details

  • Microsoft Visual C# 2010 01019-532-2002102-70826
  • Microsoft Visual C# 2010

I had the same problem in one of our projects.

We solved it by subclassing the MetroForm class and adding a function in the OnLoad event to loop through all descendant controls of type Panel and change the BackColor property.

Example:

public class CustomMetroForm : MetroForm
{
    public CustomMetroForm()
    {
        this.Load += this.OnLoad;
    }

    public virtual void OnLoad(object sender, EventArgs eventArgs)
    {
        foreach (var descendant in this.Descendants<Panel>().Where(x => x.BackColor == Color.FromArgb(255, 216, 216, 216)))
        {
            descendant.BackColor = Color.White;
        }
    }
}

public static class ControlExtensions
{
    public static IEnumerable<T> Descendants<T>(this Control control) where T : class 
    { 
        foreach (Control child in control.Controls) 
        { 
            var childOfT = child as T; 

            if (childOfT != null) 
            { 
                yield return childOfT; 
            }

            if (child.HasChildren) 
            { 
                foreach (var descendant in Descendants<T>(child)) 
                { 
                    yield return descendant; 
                } 
            } 
        } 
    }
}

Lets hope they fix this issue soon (it was not fixed as of v10.4.0.2)

Observed behavior is by design, the styling changes ambient colors of child controls so uniform look and feel can be achieved. If some controls need to be exempt from this here is description of how that is done: http://www.devcomponents.com/kb2/?p=1160#ambient

I am having no such problem with tablelayoutpanel. But for a short (and probably dirty) way you could follow is, change the color values in the form load. It will work for sure.

Issue is caused by the Metro components inside of the DevComponents suite. When adding a table layout to anything on a Metro enabled form, the table layout color resets back to whatever the stylemanager wants, regardless of what you set it as.

Have opened an issue w/ the developer to see if it can be fixed, they advise as of 10/21/2011 that they are looking at ways of overriding that issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM