开发者

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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜