C# 更改面板颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/819839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
changing panel colors
提问by tintincutes
I created a simple code here just to play C# around. It has 3 buttons and 1 panel. If you click on the 2nd & 3rd button the panel height changes. Is that also possible to change the color? For example : If I click on the 2nd button, I would like to have it as yellow and at the same time the height changes as well. and the same with 3rd button. Thanks :-)
我在这里创建了一个简单的代码来玩 C#。它有 3 个按钮和 1 个面板。如果您单击第二个和第三个按钮,面板高度会发生变化。这也可以改变颜色吗?例如:如果我点击第二个按钮,我想让它变成黄色,同时高度也会改变。与第三个按钮相同。谢谢 :-)
public partial class Form1 : Form
{
public int heightPanel;
public Form1()
{
InitializeComponent();
heightPanel = panel1.Height;
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Height = heightPanel;
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Height = this.Height/2;
}
private void button3_Click(object sender, EventArgs e)
{
panel1.Height = this.Height - 150;
}
}
I have an idea but I don't know where to put this I think it would be something like this: panel1.Height=this.BackColor.ToString(); Any inputs?
我有一个想法,但我不知道把它放在哪里我认为它会是这样的: panel1.Height=this.BackColor.ToString(); 任何输入?
Thanks
谢谢
Hi Thanks for the reply. Yes I would like to retain the 3 colors if I click in any of the button. I'm not sure if it's possible. for ex: my button1 =pink button2=yellow button3=green If I click on the button 1 I'll see the pink color and if I click on the button2 I'll see the pink and yellow.
您好,感谢您的答复。是的,如果我单击任何按钮,我想保留 3 种颜色。我不确定这是否可能。例如:我的 button1 =pink button2=yellow button3=green 如果我点击 button 1 我会看到粉红色,如果我点击 button2 我会看到粉红色和黄色。
Is that possible?
那可能吗?
Thanks again
再次感谢
采纳答案by Adam Robinson
All you'd need to do would be to set the BackColor
on a new line. For instance...
您需要做的就是将 设置BackColor
在新行上。例如...
private void button2_Click(object sender, EventArgs e)
{
panel1.Height = this.Height/2;
panel1.BackColor = Color.Yellow;
}
private void button3_Click(object sender, EventArgs e)
{
panel1.Height = this.Height - 150;
panel1.BackColor = Color.Yellow;
}
回答by Ian
Simple as:
简单如:
panel1.BackColor = Color.Red;
回答by Erich Mirabal
In button2_Click
, just add another line like this:
在 中button2_Click
,只需添加另一行,如下所示:
panel1.BackColor = Color.Yellow;