Solved

Switching between Forms in C#

Posted on 2008-10-22
3
2,063 Views
Last Modified: 2008-10-26
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

0
Comment
Question by:Swamprat1000
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 22783024
Right...each time you switch to a different form another instance is being created.  You are essentially orphaning the old one since you never go back to it.

One possible approach to this is to use the Singleton pattern.  Each form has a static GetInstance() method that returns the same single instance no matter where it was called from.  This allows you to get back the already open instance so you can show it again.

    static class Program

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(Form1.GetInstance());

        }

    }
 
 

    public partial class Form1 : Form

    {

        private Form1()

        {

            InitializeComponent();

        }
 

        private static Form1 form1Instance = null;
 

        public static Form1 GetInstance()

        {

            if (Form1.form1Instance == null)

            {

                Form1.form1Instance = new Form1();

                Form1.form1Instance.FormClosed += new FormClosedEventHandler(form1Instance_FormClosed);

            }

            return Form1.form1Instance;

        }
 

        static void form1Instance_FormClosed(object sender, FormClosedEventArgs e)

        {

            Form1.form1Instance = null;

        }
 

        private void button1_Click(object sender, EventArgs e)

        {

            Form2 f2 = Form2.GetInstance();

            f2.Show();

            this.Hide();

        }
 

    }
 
 

    public partial class Form2 : Form

    {

        private Form2()

        {

            InitializeComponent();

        }

        private static Form2 form2Instance = null;
 

        public static Form2 GetInstance()

        {

            if (Form2.form2Instance == null)

            {

                Form2.form2Instance = new Form2();

                Form2.form2Instance.FormClosed += new FormClosedEventHandler(form2Instance_FormClosed);

            }

            return Form2.form2Instance;

        }
 

        static void form2Instance_FormClosed(object sender, FormClosedEventArgs e)

        {

            Form2.form2Instance = null;

        }
 

        private void button1_Click(object sender, EventArgs e)

        {

            Form1 f1 = Form1.GetInstance();

            f1.Show();

            this.Hide();

        }
 

    }

Open in new window

0
 
LVL 8

Expert Comment

by:jorgesv13
ID: 22783195
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22783257
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
which object should i use (efficiency) 5 45
Authentication of Web Services 3 46
Error in page 3 44
Facial recognition to look through the whole database for a person 3 39
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now