C#: Background color problem

2019-07-18 10:24发布

I am having trouble with background colors in C#. For some reason they are simply not working. Specifically on the System.Windows.Forms.Panel control. Setting the BackColor property does nothing. All I have is gray and all the efforts I have made will not change it. Is there something that could be overriding this? Am I approaching this the wrong way?

4条回答
▲ chillily
2楼-- · 2019-07-18 10:51

It may be that your control is being repainted without your knowledge.

To try to rule this out, I would add a Form Load event, and force the following properties:

private void Form1_Load(object sender, EventArgs e)
{
   this.panel1.BackColor = System.Drawing.Color.Maroon; // or any other color
   this.panel1.Visible = true
}

And on the designer, I would bring the control to the foreground.

查看更多
霸刀☆藐视天下
3楼-- · 2019-07-18 11:01

I had this problem with two overlayed panels in one form. I inadvertently had one panel as a child of the other main when I created them in Design View.

查看更多
唯我独甜
4楼-- · 2019-07-18 11:04

I have had instances where controls must have both the foreground and background colors specified, you might want to try setting the foreground color explicitly as well, to see if that works.

查看更多
趁早两清
5楼-- · 2019-07-18 11:09

If you want to have the Panel Control the same color as the Form's background color, then you can use this:

myPanel.Parent = this;

... I hope this helps :)

查看更多
登录 后发表回答