C# 单击时更改按钮颜色(多次单击/颜色)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13949983/
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
Change button color when clicked (multiple clicks/colors)
提问by user1915442
I am trying to make a array of 8x8 buttons, and so far it works. Now I have stumbled upon a problem. I want the color of the button (backcolor) to change when it is clicked. And change to a different color when clicked again.
我正在尝试制作一组 8x8 按钮,到目前为止它可以工作。现在我偶然发现了一个问题。我希望按钮(背景色)的颜色在单击时发生变化。再次单击时更改为不同的颜色。
This is my code so far:
到目前为止,这是我的代码:
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Button[,] btn = new Button[8,8];
public Form1()
{
InitializeComponent();
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
btn[x,y] = new Button();
btn[x,y].SetBounds(40 * x, 40 * y, 40, 40);
btn[x,y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(btn[x, y]);
btn[x,y].BackColor = Color.Black;
}
}
/*
btn.Click += new EventHandler(this.btnEvent_click);
btn[x,y].Text = Convert.ToString(x+","+y);
Controls.Add(btn);
btn[x,y].Click += new EventHandler(this.btnEvent_click);
*/
}
private void form1_load(object sender, EventArgs e)
{
}
void btnEvent_click(object sender, EventArgs e)
{
(Control)sender).BackColor = Color.Red;
}
}
}
So far I can only change the color to red, and I've tried multiple if and for statements to change the color a second time.
到目前为止,我只能将颜色更改为红色,并且我已经尝试了多个 if 和 for 语句来再次更改颜色。
Could anyone help me out?
有人可以帮我吗?
采纳答案by Rohit Vyas
Hi Temporary you can use below solution:
嗨临时您可以使用以下解决方案:
void btnEvent_click(object sender, EventArgs e)
{
Control ctrl = ((Control)sender);
switch (ctrl.BackColor.Name)
{
case "Red":
ctrl.BackColor = Color.Yellow;
break;
case "Black":
ctrl.BackColor = Color.White;
break;
case "White":
ctrl.BackColor = Color.Red;
break;
case "Yellow":
ctrl.BackColor = Color.Purple;
break;
default:
ctrl.BackColor = Color.Red;
break;
}
}
I know there can be a better solution also, but meanwhile you can go with this...you can add more colors also in switch statment as required
我知道也可以有更好的解决方案,但同时你可以使用这个......你也可以根据需要在 switch 语句中添加更多颜色
回答by user1321471
have you tried something similar to?
你试过类似的东西吗?
void btnEvent_click(object sender, EventArgs e)
{
if ((Control)sender).BackColor != Color.Red)
(Control)sender).BackColor = Color.Red;
else
(Control)sender).BackColor = Color.Green;
}
回答by Uthistran Selvaraj
Try it.. its working...
试试看……它的工作……
Inside the button1 click event:
在 button1 单击事件中:
Random randomGen = new Random();
KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
KnownColor randomColorName = names[randomGen.Next(names.Length)];
Color randomColor = Color.FromKnownColor(randomColorName);
button1.BackColor = randomColor;
回答by Jester
you can allways make a class that inherits the button class then add a counter to it for instance how often it was clicked and then inclrease that on every click and then change the color according to that click
您始终可以创建一个继承按钮类的类,然后为其添加一个计数器,例如单击它的频率,然后在每次单击时增加该计数器,然后根据该单击更改颜色
回答by Halil Ibrahim
You can use Color.FromArgb(int) method and Random class. See below:
您可以使用 Color.FromArgb(int) 方法和 Random 类。见下文:
Random rnd = new Random();
//Alpha set allways 255
Color.FromArgb(255, rnd.Next(255), rnd.Next(255), rnd.Next(255));
//Or Completly random
Color.FromArgb(rnd.Next());
回答by Alex
You can try something like this if you want to "cycle" through a fixed set of colors:
如果您想通过一组固定的颜色“循环”,您可以尝试这样的操作:
void btnEvent_click(object sender, EventArgs e)
{
var button = (Button)sender;
switch(button.BackColor)
{
case Color.Black: { button.BackColor= Color.Red;} break;
case Color.Red: { button.BackColor= Color.Blue;} break;
case Color.Blue: { button.BackColor= Color.Green;} break;
// and so on ... don't forget the default clause, just in case
default: { button.BackColor= Color.Black;} break;
}
}
回答by Istvan Reiter
You can make a new class, tha inherit from Button and handle internally the color change, something like this:
您可以创建一个新类,从 Button 继承并在内部处理颜色更改,如下所示:
class TwoColorButton : Button
{
private int stateCounter = 0;
private Color[] states = new Color[] { Color.Black, Color.Red };
public TwoColorButton()
: base()
{
this.BackColor = states[stateCounter];
this.Click += this.clickHandler;
}
protected void clickHandler(object sender, EventArgs e)
{
stateCounter = stateCounter == 0 ? 1 : 0;
this.BackColor = states[stateCounter];
}
}