简体   繁体   中英

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. The problem is that i want to restart my form2. To restart my form2 i'm using

application.restart();

But this restart whole project and form1 appears again.

My question is that is there any way so i could able to restart my only form2 not whole application.

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.

Yes, it is expected as the method Restart is getting on Application class and that's sole purpose is to restart whole application.

You can use the Close() to programmatically invoke the close window action and then you can create new instance of your Form like:

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.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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