Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In win forms application ,I have 2 forms (form1,form2):
In form1 I have :

this.hide();
Form2.Show();

Now in form2 I want to show the previous form ,not by instancing
How can I access form1?

Form2:
this.hide();
//how can I show the first form ,Not by Instance a new !
Posted
Updated 18-Oct-10 21:45pm

You can make a static class in a new FormManager.cs file, every time you show or hide a form use this class.
Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    public class FormManager
    {
        static FormManager _instance = null;
        private Form1 f1 = new Form1();
private Form2 f2 = new Form2();
        public static FormManager Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new FormManager();

                return _instance;
            }
        }

        public void FormOne(bool flag)
        {
            if (flag == true)
                f1.Show();
            else
                f1.Hide();
        }

public void FormTwo(bool flag)
        {
            if (flag == true)
                f2.Show();
            else
                f2.Hide();
        }
    }
}

Sample code of the form
FormManager.Instance.FormOne(true);
 
Share this answer
 
Comments
Dalek Dave 19-Oct-10 5:37am    
Good Answer.
There are number of ways of doing this
but way is depend on requirement/application.

1. You can use reference of Form1 in Form2
2. You can open Form2 as DialogBox
3. you can make use of start up class to manage which form should be open.
and so on


I suggest you to say more about your question

Thanks
 
Share this answer
 
in the form1
you write

form2.ShowDialog();

then after in form2

form2.close();



or

form declare as one static class in form1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900