我正在尝试将从ColorDialog
返回的颜色值分配到另一个表单上。
表单1由2个按钮组成:“下订单”(创建带有一组控件的新表单)和“选择颜色”(允许您更改“下订单”表单的颜色)。因此,您无法同时打开“下订单”和“选择颜色”。
因此,我必须将Place Place表单的BackColor
属性引用到具有两个按钮的表单,以便ColorDialog.Color
可以分配到Place Order表单。
Form1代码:
private void SelectColor_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
string color = Convert.ToString(colorDialog1.Color);
MessageBox.Show(color);
this.BackColor = colorDialog1.Color; // BackColor is only accessible for this form
}
}
答案 0 :(得分:1)
你这样做的方式,你需要保持一个变量来保持颜色。这样做:
//Declare this private variable to hold the color selected by the user
private System.Drawing.Color selectedcolor;
private void SelectColor_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
selectedcolor = colorDialog1.Color; // BackColor stored in variable
}
}
然后在您启动新表单的代码(下订单按钮)中输入:
private void PlaceOrder_Click(object sender, EventArgs e)
{
//I am assuming PlaceOrderForm is the name of the class of your other form
PlaceOrderForm frm = new PlaceOrderForm();
//Initialize other properties and events,etc.
//Then make its background color as selected by user
if(selectedcolor != null) frm.BackColor = selectedcolor;
}
答案 1 :(得分:0)
if(colorDialog1.ShowDialog() != DialogResult.OK) {return;}
form2 f = new form2();
f.BackColor = colorDialog1.Color;
f.Show();