I have two forms ( Form1 , Form2 ). Form1 appears immediately after the program starts, is the main one and is launched via Application.Run(new Form1()) . I need to implement the transition from Form1 to Form2 and vice versa, but so that only one form is displayed on the screen. The forms have buttons, go to Form2 and return to the main form. The transition to Form2 carried out through:

 Form1.ActiveForm.Hide(); Form2 frm = new Form2(); frm.Show(); 

How do I implement a return to the main form?

    2 answers 2

    1. You can send a link to the form

       public class AppForm2 : System.Windows.Forms.Form { public Form ReturnForm; public AppForm2() { this.Size = new System.Drawing.Size(300, 300); this.Text = "AppForm2"; Button button = new Button() { Text = "AppForm2" }; button.Click += button_Click; this.Controls.Add(btn); } private void button_Click(object sender, EventArgs e) { ReturnForm.Show(); this.Close(); } } public class AppForm1 : System.Windows.Forms.Form { public AppForm1() { this.Size = new System.Drawing.Size(300, 300); this.Text = "AppForm1"; Button button = new Button() { Text = "AppForm1" }; button.Click += button_Click; this.Controls.Add(btn); } private void button_Click(object sender, EventArgs e) { this.Hide(); new AppForm2() { ReturnForm = this }.Show(); } } 
    2. You can set the event to close the form

       public class AppForm2 : System.Windows.Forms.Form { public AppForm2() { this.Size = new System.Drawing.Size(300, 300); this.Text = "AppForm2"; Button button = new Button() { Text = "AppForm2" }; button.Click += button_Click; this.Controls.Add(btn); } private void button_Click(object sender, EventArgs e) { this.Close(); } } public class AppForm1 : System.Windows.Forms.Form { public AppForm1() { this.Size = new System.Drawing.Size(300, 300); this.Text = "AppForm1"; Button button = new Button() { Text = "AppForm1" }; button.Click += button_Click; this.Controls.Add(btn); } private void button_Click(object sender, EventArgs e) { this.Hide(); Form form = new AppForm2(); form.FormClosed += (object s, FormClosedEventArgs ev) => { this.Show(); }; form.Show(); } } 
    3. You can use the dialog box

       public class AppForm1 : System.Windows.Forms.Form { public AppForm1() { this.Size = new System.Drawing.Size(300, 300); this.Text = "AppForm1"; Button button = new Button() { Text = "AppForm1" }; button.Click += button_Click; this.Controls.Add(btn); } private void button_Click(object sender, EventArgs e) { this.Hide(); new AppForm2().ShowDialog(); this.Show(); } } 
    4. You can write through ApplicationContext, then you can not hide forms, but switch between them. In this case, you can transfer the logic to a separate class.

       public class ApplicationManager { private static ApplicationContext _context; public static ApplicationContext Context { get { if (_context == null) _context = new ApplicationContext(); return _context; } } public static void ShowForm(Form form) { Form prev = Context.MainForm; Context.MainForm = form; prev.Close(); form.Show(); } } public class AppForm2 : System.Windows.Forms.Form { public AppForm2() { this.Size = new System.Drawing.Size(300, 300); this.Text = "AppForm2"; Button button = new Button() { Text = "AppForm2" }; button.Click += button_Click; this.Controls.Add(btn); } private void button_Click(object sender, EventArgs e) { ApplicationManager.ShowForm(new AppForm1()); } } public class AppForm1 : System.Windows.Forms.Form { public AppForm1() { this.Size = new System.Drawing.Size(300, 300); this.Text = "AppForm1"; Button button = new Button() { Text = "AppForm1" }; button.Click += button_Click; this.Controls.Add(btn); } private void button_Click(object sender, EventArgs e) { ApplicationManager.ShowForm(new AppForm2()); } } static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); ApplicationManager.Context.MainForm = new AppForm1(); Application.Run(ApplicationManager.Context); } 

    In general, you can come up with other options or improve the short examples shown to fit your needs. In any case, it all comes down to events or links on a form, only ApplicationContext is of particular interest.

    • I'll try to figure it out, I hope I will achieve the result I need - forge456
    • And if my forms are not in one cs file, but in several. I created them through (Project-> Add Windows Form). That is, respectively, Form1.cs and Form2.cs. I consider the first proposed option with a link and cannot use the ReturnForm field in the Form2.cs file code - forge456
    • The second and third options will not work for me, since there will be more forms later and transitions from the second form to the next will be needed. I will try to disassemble the last option, but so far there is little to understand. - forge456
    • @ forge456, it makes no difference where the forms are located or how they were created, here they are presented in this form only to save space. If many forms are planned, then the first / fourth option is better or UserControls are added as below. Although it may be necessary to use explicitly in the first property of the public Form ReturnForm {get; set;} public Form ReturnForm {get; set;} , although I don't think the problem is this. - Alex Krass
    • In general, I was digging for a long time and could not understand why I could not use the ReturnForm field, I found an error. Everything is working. Thank you. I simply simply did not try to use this field there. - forge456

    For WinForms (for WPF I’m not sure, correct it if I’m mistaken) you can refuse at all from individual forms in the application, with the exception of dialogs and child forms that open not instead of, but together with the main one.

    To do this, you need to fill the forms (including the main one) into the UserControl and display them in the necessary sequence on the main form. From the pros: the application forms do not jump across the screen, the logic of closing the application is in one place, you can set a single menu in the main form for all child form controls.

    The rest is described in detail by @Alex Krass

    • So you propose to implement the creation and configuration of a form completely programmatically? - forge456
    • @ forge456 if nothing changes for the user, then why complicate your life? - rdorn
    • @ forge456 besides, UserControl does not need to be re-created each time. You can create them in the constructor as a form and use where necessary. - rdorn