Hide and restore the GUI Mutiple forms in c#

I have multiple form application. Form 1 is Login form for user validation. Form1 goes to Form2(Menu form). Form 2 leads to Form3 which is only popupform and hides the form2 when it is open.Form 3 goes to Form 4. Now from Form4 ,with button click, I need to restore the Form2 without creating a new instance. I tried using singelton approach, getting error.Code as follows as described above.

Form1:

private void click_Click(object sender, EventArgs e)

{

if ((user.Text == username) && (pswd.Text == password))

{ Form2 menu = new Form2();

            menu.Username = user.Text;
            //hides the form1
            this.Hide();      
            menu.ShowDialog();
        }
    }

Form2:

private static Form2 instance;

    public static Form2 Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Form2();
            }
            return instance;
        }
    }

private void button_Click(object sender, EventArgs e) {

        //Hide the form2
        Hide();
        //Bring up your PopUp form
        using (Form3 form3 = new Form3())
            form3.ShowDialog();

    }

Form3:

private void button_Click(object sender, EventArgs e)

{
        Hide();
        Form4 form4 = new Form4();
        form4.Show();
    }

Form4:

private void button1_Click(object sender, EventArgs e)

{

        Form2 form2 = Form2.Instance;//error occured as

Mainmenu does not contain a reference for Instance and no extension method accepting a first argument of type 'Mainmenu'

    }

Basically, I want to restore the Form2 that was hided.Any help would be appreciated!Thanks


How about keeping track of the Forms parents so you have direct access. For example in your Form1:

menu.Parent = this;
Hide();
menu.ShowDialog();

Then in your Form2 when Form3 is called:

using (Form3 f3 = new Form3())
{
    Hide();
    f3.Parent = this.Parent;
    f3.ShowDialog();
}

Then when you want to dispose of Form3:

this.Parent.Show();
this.Parent.Focus();
this.Dispose();

Something along this lines for as many open forms as you need. If you need to drop back to the previous form, then make the Parent 'this' instead of 'this.Parent'

链接地址: http://www.djcxy.com/p/21004.html

上一篇: c#我怎样才能得到我的printPreviewControl1打印到PDF的值

下一篇: 隐藏和恢复GUI c#中的多个表单