从另一个表单更改表单字体颜色

时间:2012-04-07 05:10:26

标签: c# forms colors fonts

我有一个表单让我们称之为formMain,它有一个菜单可以打开另一个名为formColors的表单。 现在当formColors打开时,它的单选按钮标有Colors,即红色

如何将formMain的字体颜色更改为我在formColors中选择的任何选项?

1 个答案:

答案 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; }
    }
}