简体   繁体   English

如何从另一个表单刷新一个表单?

[英]How to Refresh a form from another form?

I have two forms- form1 and form2. 我有两种形式-form1和form2。 I call form2 from form1 using the below snippet: 我使用以下代码片段从form1调用form2:

Application.run(new Form2());

Form2 f2=new Form2();

f2.show();

This code is working absolutely fine and i could see the form2 loading from form1. 这段代码工作正常,我可以看到从form1加载form2。

Now i need to repeat this for say 5 times. 现在我需要重复5次。 When i run it for 1st time, New istance has to be created ( ie 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. 当我第一次运行它时,必须创建新的距离(即必须执行上面的代码),但是当我第二次/第三次或第四次运行它时,我需要刷新form2而不是创建一个新的form2。 When i run the above code for 5 times, 5 new instances of form2 are created. 当我将上述代码运行5次时,将创建5个新的form2实例。 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. 相反,form2只必须显示一次,但是当我调用除第一种形式之外的其他时间时,必须刷新它。 Can you please throw some light on this? 你能对此有所启发吗?

. When i run the above code for 5 times, 5 new instances of form2 are created. 当我将上述代码运行5次时,将创建5个新的form2实例。 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. 相反,form2只必须显示一次,但是当我调用除第一种形式之外的其他时间时,必须刷新它。

This is because you create it 5 times: 这是因为您创建了5次:

 Form2 f2=new Form2();

This create a new Form. 这将创建一个新的窗体。 If you call it 5 times it will create 5 instances of form. 如果调用5次,它将创建5个form实例。

If you want to refresh the data, you need to create a public method "Refresh()" and call it. 如果要刷新数据,则需要创建一个公共方法“ Refresh()”并调用它。 For example: 例如:

f2.Refresh();

This way, you will be able to redo the binding or set again the data you want to refresh for the persistence. 这样,您将能够重做绑定或再次设置要刷新以保持持久性的数据。

you can try like this .... 你可以这样尝试...

please note that this is only a rudimentary implementation of a Mediator pattern. 请注意,这只是Mediator模式的基本实现。 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);
    }
}

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.... 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM