简体   繁体   English

C#WinForm:提交另一个表单后重新加载表单?

[英]C# WinForm : Reload form after submit another form?

I'm newbie of winform. 我是winform的新手。 I have opened form2 form a linklabel in form1 using : 我已经使用以下形式在form1中打开了form2 form linklabel:

private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    FrmAddMov frmAddMov = new FrmAddMov();
    if(frmAddMov.ShowDialog() == DialogResult.OK)
    {
        this.Invalidate();
        //or
        this.Refresh();
    }
}

I thought form1 will reload after I submit form2, but not. 我以为form1将在我提交form2之后重新加载,但不是。 Please tell me the right way to do it. 请告诉我正确的方法。 Thanks a lot, and sorry if my english is too bad. 非常感谢,如果我的英语太差了,请对不起。

Move everything in your form load event to a method say FormLoad. 将您的表单加载事件中的所有内容移到一个名为FormLoad的方法上。 You may want to add few other statements which you are expecting form reload will do for you. 您可能需要添加一些其他语句,以期望重新加载表单会为您完成。 Call this method when your 2nd form closes. 当您的第二个表单关闭时,调用此方法。

Something like this 像这样

private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        FrmAddMov frmAddMov = new FrmAddMov();
        if(frmAddMov.ShowDialog() == DialogResult.OK)
        {
           FormLoad();
        }
    }

Gentleman's answer will work, but it can be improved. 绅士的答案会起作用,但可以改善。
When showing a form using ShowDialog than it is best practice to dispose of that form, and the easiest way to do that is by the using statement 使用ShowDialog显示表单时,最好的方法是处理该表单,最简单的方法是using语句

private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    using (FrmAddMov frmAddMov = new FrmAddMov())
    {
        if (frmAddMov.ShowDialog() == DialogResult.OK)
        {
           FormLoad();
        }
    }
}

This way you are always 100% sure that all resources for frmAddMov will be cleaned up. 这样,您始终可以100%确保会清除frmAddMov的所有资源。

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

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