如何从另一个表单刷新表单? [英] How to Refresh a form from another form?

查看:32
本文介绍了如何从另一个表单刷新表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表单——form1 和 form2.我使用以下代码段从 form1 调用 form2:

I have two forms- form1 and form2. I call form2 from form1 using the below snippet:

Application.run(new Form2());

Form2 f2=new Form2();

f2.show();

这段代码工作得很好,我可以看到从 form1 加载了 form2.

This code is working absolutely fine and i could see the form2 loading from form1.

现在我需要重复这个说 5 次.当我第一次运行它时,必须创建新的实例(即必须执行上面的代码)但是当我第二次/第三次或第四次运行它时,我需要刷新form2而不是创建一个新的form2.当我运行上述代码 5 次时,会创建 5 个 form2 的新实例.相反,form2 必须只显示一次,但是当我调用第一个表单以外的任何其他时间时必须刷新它.能否请您解释一下?

Now i need to repeat this for say 5 times. When i run it for 1st time, New istance has to be created ( i.e above code has to be executed) but when i run it for 2nd/3rd or 4th time, i need to refresh the form2 instead of creating a new form2. When i run the above code for 5 times, 5 new instances of form2 are created. Instead the form2 has to be displayed only once but it has to be refreshed when i call anyother time other than the first form. Can you please throw some light on this?

推荐答案

你可以这样试试....

you can try like this ....

请注意,这只是 Mediator 模式的基本实现.我强烈建议您阅读有关该模式和一般设计模式的内容,以便更好地了解正在发生的事情.

please note that this is only a rudimentary implementation of a Mediator pattern. I would highly encourage you to read up about that pattern, and design patterns in general in order to gain a better understanding of what is going on.

同样,这是一个示例,但它确实有一些基本的错误检查,应该可以帮助您.

Again, this is a sample, but it does have some basic error checking and should get you going.

您的表单声明将如下所示:

Your form declaration is going to look something like this:

public partial class MainForm : Form
{
    private FormMediator _formMediator;

    public MainForm()
    {
        InitializeComponent();
    }

    public void SomeMethodThatOpensTheSubForm()
    {
        SubForm subForm = new SubForm();

        _formMediator = new FormMediator(this, subForm);

        subForm.Show(this);
    }
}

Mediator 的修改实现如下所示:

And the modified implementation of the Mediator would look like this:

public class FormMediator
{
    private Form _subForm;
    private Form _mainForm;

    public FormMediator(Form mainForm, Form subForm)
    {
        if (mainForm == null)
            throw new ArgumentNullException("mainForm");

        if (subForm == null)
            throw new ArgumentNullException("subForm");

        _mainForm = mainForm;
        _subForm = subForm;

        _subForm.FormClosed += MainForm_FormClosed;
    }

    void MainForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        try
        {
            _mainForm.Refresh();
        }
        catch(NullReferenceException ex)
        {
            throw new InvalidOperationException("Unable to close the Main Form because the FormMediator no longer has a reference to it.", ex);
        }
    }
}

希望对你有帮助....

I hope it helps you....

这篇关于如何从另一个表单刷新表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆