从不同的表单

时间:2015-07-13 03:42:49

标签: c# user-interface controls updates

我有一个主窗体(frm_Main)和设置窗体(frm_Settings),我想从frm_Settings更新frm_Main中的控件。到目前为止,我已经尝试在frm_Main中添加一个方法,然后从frm_Settings访问它,希望它能够正常工作,但事实并非如此。这是我当前方法的代码:

主要表格

public partial class frm_Main : Form
{
    public frm_Main()
    {

    }

    public void ChangeBackColor(Color color)
    {
        richTextBox.BackColor = color;
    }
}

设置表单

public partial class frm_Settings : Form
{
    public frm_Settings()
    {

    }

    private void pbcl_editorBackColor_Click(object sender, EventArgs e)
    {
        ColorDialog editorBackColor = new ColorDialog();
        if (editorBackColor.ShowDialog() == DialogResult.OK)
        {
            Variables.Editor_BackColor = "#" + editorBackColor.Color.ToArgb().ToString("X");
            Color colour = ColorTranslator.FromHtml(Variables.Editor_ForeColor);
            var Main = new frm_Main();
            Main.ChangeBackColor(colour);
        }
    }
}

单击ColorSelectorDialog上的“确定”按钮,没有任何反应,没有任何更新。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

试试这个

Settings.cs

public partial class frm_Settings : Form
{
    private frm_Main _main;
    public frm_Settings(frm_Main main)
    {
          _main = main;
    }

    private void pbcl_editorBackColor_Click(object sender, EventArgs e)
    {
        ColorDialog editorBackColor = new ColorDialog();
        if (editorBackColor.ShowDialog() == DialogResult.OK)
        {
            Variables.Editor_BackColor = "#" + editorBackColor.Color.ToArgb().ToString("X");
            Color colour = ColorTranslator.FromHtml(Variables.Editor_ForeColor);                
            _main.ChangeBackColor(colour);
        }
    }
}

Main.cs

public partial class frm_Main : Form
{
    public frm_Main()
    {

    }
    private void LaunchSetting()
    {
       var settings = new frm_Settings(this);
       settings.ShowDialog();
    }
    public void ChangeBackColor(Color color)
    {
        richTextBox.BackColor = color;
    }
}

编辑:

我们也可以处理它来创建一个事件

Settings.cs

public partial class frm_Settings : Form
{
    public delegate void ColorChangedHandler(Color color);
    public event ColorChangedHandler OnColorChangedHandler;
    public frm_Settings()
    {

    }

    private void pbcl_editorBackColor_Click(object sender, EventArgs e)
    {
        ColorDialog editorBackColor = new ColorDialog();
        if (editorBackColor.ShowDialog() == DialogResult.OK)
        {
            Variables.Editor_BackColor = "#" + editorBackColor.Color.ToArgb().ToString("X");
            Color colour = ColorTranslator.FromHtml(Variables.Editor_ForeColor);
            if (OnColorChangedHandler != null)
            {
               OnColorChangedHandler(colour);
            }
        }
    } 
}

Main.cs

public partial class frm_Main : Form
{
    public frm_Main()
    {

    }
    private void LaunchSetting()
    {
       var settings = new frm_Settings(this);
       settings.OnColorChangedHandler += OnColorChanged;
       settings.ShowDialog();
    }
    private void OnColorChanged(Color color)
    {
       ChangeBackColor(color);
    }
    public void ChangeBackColor(Color color)
    {
        richTextBox.BackColor = color;
    }
}