如何将 ColorDialog.Color 分配给 C# 中的另一个窗体?
我正在尝试将从一个表单上的 ColorDialog
返回的颜色值分配给另一个表单。
表单 1 由 2 个按钮组成:“下订单”(创建带有一堆控件的新表单)和“选择颜色”(允许您更改“下订单”表单的颜色)。因此,您不能同时打开“下订单”和“选择颜色”。
因此,我必须以某种方式引用“下订单”表单的 BackColor
属性来创建具有两个按钮的表单,以便可以将 ColorDialog.Color
分配给“下订单”表单。
表格 1 代码:
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
}
}
I am trying to assign the color value returned from ColorDialog
on one form to another form.
Form 1 consists of 2 buttons: 'Place Order' (creates a new form with bunch of controls) and 'Select Color' (allows you to change the color of Place Order form). So you can't have Place Order and Select Color open at the same time.
Therefore, I somehow must reference the BackColor
property of the Place Order form to form that has the two buttons so that ColorDialog.Color
can be assigned to the Place Order form.
Form1 Code:
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做的方式是,您需要维护一个变量来保存颜色。这样做:
然后在启动新表单(下订单按钮)的代码中输入:
The way you are doing this, you will need to maintain a variable to hold the color. Do it like this:
then in the code where you launch your new form (Place order button) put this: