我正在尝试让用户从颜色对话框中选择颜色,然后让按钮重新着色为他们选择的所选颜色。到目前为止,这是我的代码: 例如,如果我选择红色,我的消息框会相应地显示(红色:255,绿色:0,蓝色:0),但我的按钮变为黄色。我尝试过其他颜色,但似乎是随机的。请告诉我需要解决的问题,谢谢!
private void button14_Click(object sender, EventArgs e)
{
ColorDialog cdlg = new ColorDialog();
cdlg.ShowDialog();
System.Drawing.Color clr = cdlg.Color;
System.String r = System.String.Empty;
System.String g = System.String.Empty;
System.String b = System.String.Empty;
try
{
byte red = clr.R;
byte blue = clr.B;
byte green = clr.R;
byte a = clr.A;
r = clr.R.ToString();
g = clr.G.ToString();
b = clr.B.ToString();
System.Drawing.Color backgroundColor = System.Drawing.Color.FromArgb(a,red,green,blue);
// show the value in message box
MessageBox.Show("Red :" + r + ", Green :" + g + ", Blue :" + b);
button14.BackColor = backgroundColor;
}
catch (System.Exception ex)
{
//doing nothing
}
}
答案 0 :(得分:1)
更简单的方法是简单地使用返回的Color
本身,因为您不能修改任何值:
private void button14_Click(object sender, EventArgs e)
{
var cdlg = new ColorDialog();
if (cdlg.ShowDialog() == DialogResult.OK)
{
button14.BackColor = cdlg.Color;
}
}