Link to home
Start Free TrialLog in
Avatar of Swamprat1000
Swamprat1000Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Switching between Forms in C#

Hi All,
Have a quick question I suspect, I would like to know the correct method of switching between forms like you would in an installation wizard when you hit the next button. Is this the act of switching between forms of hiding or showing new panels?
The reason I ask is because I do not want to use a tabbed interface and want to keep the GUI as simple as possible for the user.  The code I have here more or less works for get from the intiall Form1 to Form 2 via a button press:

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 temp = new Form2();
            temp.Region = this.Region;
            temp.Show();
            this.Hide();
        }

and to go back to Form1 from Form2 via another button press:

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 temp1 = new Form1();
            temp1.Region = this.Region;
            temp1.Show();
            this.Hide();
        }


What i dont know is what is happening in the background and if this is just making 100's of threads and closing them etc..  hence, im looking for the correct procedure to be used when wanting to achieve the ability to switch between forms as described above.

Many Thanks,
Richard
Form 1 Button:
 
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 temp = new Form2();
            temp.Region = this.Region;
            temp.Show();
            this.Hide();
        }
 
 
 
Form 2 Button:
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 temp1 = new Form1();
            temp1.Region = this.Region;
            temp1.Show();
            this.Hide();
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also close the current Form after opening the new one:

 Form1 temp1 = new Form1();
temp1.Region = this.Region;
temp1.Show();
this.Close();

Of course, you will have to change the "Shutdown Mode" ( in the Project Properties ), so that the application closes when last form closes.

Another approach you could try is to have a single form with different panels, for each fo the "wizard" steps". Then you can turn on/off the visibility of the panels as the user navigates on the wizard.
If you have data on each form that must persist as you switch between them (like info placed into TextBoxes) then the singleton has a great advantage...you can get references to ALL of the forms at the end using GetInstance() and the data will still be there.