我有一个表单,我们称之为 formMain,它有一个菜单,可以打开另一个名为 formColors 的表单。现在,当 formColors 打开时,它具有标有颜色的单选按钮,即红色
如何将 formMain 的字体颜色更改为我在 formColors 中选择的任何选项?
创建一个Event
informColors
并订阅它,formMain
或者正如 Hans Passant 指出的那样,您可以使用公共属性和 Forms DialogResult 属性,这将是首选方法。
WinForm DialogResult 示例
表格1
public partial class Form1 : Form
{
Form2 frm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
frm2 = new Form2();
if (frm2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
this.BackColor = frm2.formColor;
frm2.Close();
}
}
表格2
public partial class Form2 : Form
{
Color newColor;
public Form2()
{
InitializeComponent();
}
public Color formColor
{
get { return this.newColor; }
set { this.newColor = value; }
}
private void btnRed_Click(object sender, EventArgs e)
{
newColor = Color.Red;
}
private void btnBlue_Click(object sender, EventArgs e)
{
newColor = Color.Blue;
}
private void btnOk_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
WinForm 事件示例:
表格1:
public partial class Form1 : Form
{
Form2 frm2 = new Form2();
public Form1()
{
InitializeComponent();
frm2.ColorEvent += new ColorEventHandler(frm2_ColorEvent);
}
void frm2_ColorEvent(object sender, ColorEventArgs e)
{
this.BackColor = e.formColor;
}
private void button1_Click(object sender, EventArgs e)
{
frm2.ShowDialog();
}
}
表格2:
public delegate void ColorEventHandler(object sender, ColorEventArgs e);
public partial class Form2 : Form
{
public event ColorEventHandler ColorEvent;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ColorEventArgs newColor = new ColorEventArgs();
newColor.formColor=Color.Red;
ColorEvent(this, newColor);
}
}
颜色事件类
public class ColorEventArgs : EventArgs
{
private Color newColor;
public Color formColor
{
get { return this.newColor; }
set { this.newColor = value; }
}
}