I was looking something similar with winforms like
// in Form 1
this.Hide();
Form2 form = new Form2();
form.Show
// in Form 2
// if button pressed, Form 1 will be displayed, while Form 2 will be Hide.
I was trying my luck for FormEventHandler but doesn’t know where to start.
Any suggestions/ideas?
(For the purposes of this answer,
Form1andForm2represent classes inheriting fromWindows.Window)I would recommend one of the following approaches
Approach 1: Keeping
Form2alive and able to show againIn this case, you’ll need to make an instance variable on
Form1:In your code to “switch” to
Form2, do this:Then add this function to
Form1:Now all you need to do is call
Hide();withinForm2to have it switch back toForm1.Approach 2: Closing
Form2and opening a new instance each timeThis is a bit easier, and more in line with what you have:
Similarly, add this function to
Form1:Now, instead of calling
Hide();inForm2, callClose();