问题
I need help with my tool.
I have try change Color
my Panel
with ColorDialog
but, it does not work
I want change colors all Panel
in my Form
.
Panel constuctor:
Panel p = new Panel();
Event handlers:
private void button104_Click_1(object sender, EventArgs e)
{
this.bg.FullOpen = true;
if (this.bg.ShowDialog() == DialogResult.OK)
{
this.setBgColor(this.bg.Color);
}
}
public void setBgColor(Color rgb)
{
p.BackColor = rgb;
}
回答1:
You can select all controls of a particular type by using the System.Linq
extension method, OfType
, and if you iterate over them in a loop, you can set all their BackColor
properties:
private void button1_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog() == DialogResult.OK)
{
foreach (var panel in Controls.OfType<Panel>())
{
panel.BackColor = cd.Color;
}
}
}
Note that this only iterates over the controls belonging directly to the form itself. If any of the panels are inside a container control, then we will need to look through each control to see if it contains any panels.
We can write a helper method for this that takes in a Control
to inspect, a Color
to use for the BackColor
, and a Type
that specifies the type of control we want to set the back color for.
Then we first check if the Control
is the same type as the one we're looking for, and if it is, set it's backcolor. After that, we loop through all it's children and recursively call the method again on them. This way, if we pass the parent form as the Control
, we will iterate through all the controls:
private void SetBackColorIncludingChildren(Control parent, Color backColor, Type controlType)
{
if (parent.GetType() == controlType)
{
parent.BackColor = backColor;
}
foreach(Control child in parent.Controls)
{
SetBackColorIncludingChildren(child, backColor, controlType);
}
}
private void button1_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog() == DialogResult.OK)
{
// Pass 'this' to the method, which represents this 'Form' control
SetBackColorIncludingChildren(this, cd.Color, typeof(Panel));
}
}
回答2:
You can use this:
private void button1_Click(object sender, EventArgs e)
{
this.bg.FullOpen = true;
if ( this.bg.ShowDialog() == DialogResult.OK )
{
setBgColor(Controls, bg.Color);
}
}
public void setBgColor(Control.ControlCollection controls, Color rgb)
{
foreach ( Control control in controls )
{
if ( control is Panel )
( (Panel)control ).BackColor = rgb;
setBgColor(control.Controls, rgb);
}
}
It parses all controls of the form and for each it parses all controls of the controls recursively.
Then the color of all panels of the form is changed, all "root" panels and all panels in panels in panels in panels...
回答3:
This looks like winforms. Assuming this is the case, you need to iterate over all the controls on the form, and for each that is a Panel
, set its color.
Something like this (untested, you may need to play about a bit )
public void setBgColor(Color rgb)
{
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(System.Windows.Forms.Panel))
{
c.BackColor = rgb;
}
}
}
来源:https://stackoverflow.com/questions/58011018/how-to-change-backcolor-of-all-my-panel-in-my-form