如何获取按钮的背景色并将其发送为其他形式? [英] How can I get button's background color and send it to other form?
问题描述
我有一个包含2个表单的项目,第一个表单为空.第二种形式有5个按钮.
I have a project with 2 forms, the first form is empty. The second form has 5 buttons.
当我按下一个按钮时,它会打开一个颜色对话框.我正在选择一种颜色,并且按钮的背景色正在改变.例如,如果 button1
的背景色ID为 Green
,则 form1
的背景色应为绿色.不是即时的,而是当我按下保存按钮时.我需要将按钮的背景色转换为变量.我该怎么办?
When I press a button, it opens a color dialog. I'm choosing a color, and button's background color is changing. For example, if button1
's background color id is Green
, form1
's background color should be green. Not instant but when I press the Save button. I need to get button's background color into a variable. How can I do this?
private void btnKAMU_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
btnKAMU.BackColor = colorDialog1.Color;
}
推荐答案
您可以将 Form1
的引用传递给 Form2
设置 Form2
所有者
,自定义属性或使用 Form2
构造函数.
You can pass a reference of Form1
to Form2
setting Form2
Owner
, with a custom Property or using Form2
constructor.
在 Form1
中创建 Form2
的实例时:
When, in Form1
, you create an instance of Form2
:
然后使用所有者属性:
(以这种方式实例化表单时,设置了 Form2
Owner
: form2.Show(this);
.此
参考是 Form2
Using then Owner property:
(Form2
Owner
is set when you instantiate a Form this way:
form2.Show(this);
. The this
reference is Form2
Owner - Form1
here).
Form2 form2 = new Form2();
form2.Show(this);
//form2.ShowDialog(this);
在 Form2
中,设置 Owner
BackColor
属性:
In Form2
, set the Owner
BackColor
property:
private void btnSAVE_Click(object sender, EventArgs e)
{
this.Owner.BackColor = btnKAMU.BackColor;
}
使用自定义属性:
Form2 form2 = new Form2();
form2.Form1Reference = this;
form2.Show();
//form2.ShowDialog();
在 Form2
中,使用属性值:
In Form2
, using a property value:
public Form Form1Reference { get; set; }
private void btnSAVE_Click(object sender, EventArgs e)
{
this.Form1Reference.BackColor = btnKAMU.BackColor;
}
在
Form2
构造函数中设置 Form1
的引用:Setting a reference of
Form1
in Form2
constructor:Form2 form2 = new Form2(this);
form2.Show();
//form2.ShowDialog();
具有与以前相同的属性值:
With a property value as before:
private Form Form1Reference { get; set; }
public Form2(Form Form1Instance)
{
this.Form1Reference = Form1Instance;
InitializeComponent();
}
private void btnSAVE_Click(object sender, EventArgs e)
{
this.Form1Reference.BackColor = btnKAMU.BackColor;
}
或将 Form1
引用分配给私有字段:
Or assign the Form1
reference to a private Field:
private Form Form1Reference;
public Form2(Form Form1Instance)
{
this.Form1Reference = Form1Instance;
InitializeComponent();
}
private void btnSAVE_Click(object sender, EventArgs e)
{
this.Form1Reference.BackColor = btnKAMU.BackColor;
}
根据您的上下文,可能有必要将选定的颜色关联到私有字段,并使用其值更改 Form1.BackColor
Depending on your context, it could be necessary to assing the chosen Color to a private Field and use its value to change Form1.BackColor
private Color Form1BackColor;
private void btnKAMU_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
btnKAMU.BackColor = colorDialog1.Color;
this.Form1BackColor = btnKAMU.BackColor;
}
根据需要使用此值更改以前的代码.
Change the previous code using this value if needed.
这篇关于如何获取按钮的背景色并将其发送为其他形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!