从不同的形式更新控件

本文关键字:更新 控件 | 更新日期: 2023-09-27 18:02:31

我有一个主表单(frm_Main)和设置表单(frm_Settings),我想更新一个控件,这是在frm_Main,从frm_Settings。到目前为止,我尝试在frm_Main中添加一个方法,然后从frm_Settings中访问该方法,希望它能工作,但它没有。下面是我当前方法的代码:

主要形式

public partial class frm_Main : Form
{
    public frm_Main()
    {
    }
    public void ChangeBackColor(Color color)
    {
        richTextBox.BackColor = color;
    }
}
<<p> 设置形式/strong>
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上的"OK"按钮后,什么都没有发生,没有任何更新。我哪里做错了?

从不同的形式更新控件

试试这个

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);
        }
    }
}

c

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);
            }
        }
    } 
}

c

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