[英]Changing a forms font color from another form
I have a form let's call it formMain that has a menu which opens another form called formColors.我有一个表单,我们称之为 formMain,它有一个菜单,可以打开另一个名为 formColors 的表单。 Now when formColors opens it has radio buttons labeled with Colors ie Red现在,当 formColors 打开时,它有标有 Colors 的单选按钮,即红色
How can I change the font color of formMain to whichever option I choose in formColors?如何将 formMain 的字体颜色更改为我在 formColors 中选择的任何选项?
Create an Event
in formColors
and subscribe to it in formMain
or as Hans Passant pointed out you could use a public property and the Forms DialogResult Property this would be the preferred way to do it.在formColors
中创建一个Event
并在formMain
中订阅它,或者正如 Hans Passant 指出的那样,您可以使用公共属性和 Forms DialogResult 属性,这将是首选的方法。
WinForm DialogResult Example WinForm 对话框结果示例
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();
}
}
Form2表格二
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 Event Example: 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();
}
}
Form2:表格二:
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; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.