如何将 ColorDialog.Color 分配给 C# 中的另一个窗体?

发布于 2024-10-16 06:14:08 字数 625 浏览 2 评论 0原文

我正在尝试将从一个表单上的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把昨日还给我 2024-10-23 06:14:08

这样做的方式是,您需要维护一个变量来保存颜色。这样做:

//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;
}

The way you are doing this, you will need to maintain a variable to hold the color. Do it like this:

//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
    }
}

then in the code where you launch your new form (Place order button) put this:

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;
}
层林尽染 2024-10-23 06:14:08
if(colorDialog1.ShowDialog() != DialogResult.OK) {return;}

form2 f = new form2();
f.BackColor = colorDialog1.Color;
f.Show();
if(colorDialog1.ShowDialog() != DialogResult.OK) {return;}

form2 f = new form2();
f.BackColor = colorDialog1.Color;
f.Show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文