简体   繁体   English

如何在C#中的WPF中(重置||重新启动||清除)form2

[英]How to (reset || restart || clear) form2 in WPF in C#

My application is consist of two forms. 我的应用程序由两种形式组成。

Form1 and Form2

Currently I'm facing problem in form2. 目前,我在form2中遇到问题。 The problem is that i want to restart my form2. 问题是我想重新启动form2。 To restart my form2 i'm using 要重新启动我的form2,我正在使用

application.restart();

But this restart whole project and form1 appears again. 但是,此重新启动整个项目和form1再次出现。

My question is that is there any way so i could able to restart my only form2 not whole application. 我的问题是有什么办法可以让我重新启动我唯一的form2而不是整个应用程序。

Below is the simple code for explanation. 以下是用于解释的简单代码。

private void btn_restart_Click(object sender, EventArgs e)
        {

            {
                Application.Restart(); //Restart whole project :(


            }
}

But this restart whole project and form1 appears again. 但是,此重新启动整个项目和form1再次出现。

Yes, it is expected as the method Restart is getting on Application class and that's sole purpose is to restart whole application. 是的,可以预期,因为Restart方法将进入Application类,并且其唯一目的是重新启动整个应用程序。

You can use the Close() to programmatically invoke the close window action and then you can create new instance of your Form like: 您可以使用Close()以编程方式调用关闭窗口操作,然后可以创建Form的新实例,例如:

private void btn_restart_Click(object sender, EventArgs e)
{

  form2.Close();  // close it
  form2 = new Form2(); // reopen it
  form2.Show(); // show on the screen

}

If you Form2 expects parameters in constructor, then you of-course would need to pass those, assuming that it has parameter-less constructor while answering this. 如果Form2在构造函数中需要参数,那么您当然需要传递这些参数,前提是在回答此问题时它具有无参数的构造函数。

Hope it helps. 希望能帮助到你。

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

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