我有一个表单让我们称之为formMain,它有一个菜单可以打开另一个名为formColors的表单。 现在当formColors打开时,它的单选按钮标有Colors,即红色
如何将formMain的字体颜色更改为我在formColors中选择的任何选项?
答案 0 :(得分:2)
在formColors
中创建Event
并在formMain
订阅,或者Hans Passant指出您可以使用公共财产和表格DialogResult Property这将是首选方式。
WinForm DialogResult示例
<强> Form1中强>
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事件示例:
<强> Form1中:强>
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);
}
}
<强> ColorEventClass 强>
public class ColorEventArgs : EventArgs
{
private Color newColor;
public Color formColor
{
get { return this.newColor; }
set { this.newColor = value; }
}
}