Peter Ladd
Peter Ladd

Reputation: 61

Hiding a form, switch to a second form, close second form and unhide first form

I've looked at all the suggested answers and nothing seems to fit what I'm looking for. I want to call a second form from my main form, hide my main form while the second form is active, and then unhide the main form when the second form closes. Basically I want to "toggle" between the two forms.

So far I have:

In my main form:

private void countClick(object sender, EventArgs e)
{
    this.Hide(); 
    subForm myNewForm = new subForm();
    myNewForm.ShowDialog();
}

and in my second form I have:

private void totalClick(object sender, EventArgs e)
{
    this.Close();
}

How do I get the main form to show?

Upvotes: 2

Views: 3940

Answers (5)

Hans Passant
Hans Passant

Reputation: 941208

This is difficult to do correctly. The issue is that you must avoid having no window at all that can get the focus. The Windows window manager will be forced to find another window to give the focus to. That will be a window of another application. Your window will disappear behind it.

That's already the case in your existing code snippet, you are hiding your main window before showing the dialog. That usually turns out okay, except when the dialog is slow to create. It will definitely happen when the dialog is closed.

So what you need to do is hide your window after you display the dialog and show it again before the dialog closes. That requires tricks. They look like this:

private void countClick(object sender, EventArgs e)
{
    this.BeginInvoke(new Action(() => this.Hide()));
    using (var dlg = new subForm()) {
        dlg.FormClosing += (s, fcea) => { if (!fcea.Cancel) this.Show(); };
        if (dlg.ShowDialog() == DialogResult.OK) {
            // etc...
        }
    }
}

The BeginInvoke() call is a trick to get code to run after the ShowDialog() method runs. Thus ensuring your window is hidden after the dialog window is shown. The FormClosing event of the dialog is used to get the window to be visible again just before the dialog closes.

Upvotes: 1

diego
diego

Reputation: 3

If you are working in the same namespace, you have the context, using mainform or the name you gave the "main form", try:

mainform.show();

Upvotes: 0

Edper
Edper

Reputation: 9322

Let's say in Form1 you click a Button to show Form2

 Form2 frm2 = new Form2();
 frm2.Activated += new EventHandler(frm2_Activated); // Handler when the form is activated
 frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed); // Hander when the form is closed
 frm2.Show();

Now, this one is when the Form2 is shown or is Activated you hide the calling form, in this case the Form1

    private void frm2_Activated(object sender, EventArgs e)
    {
        this.Hide(); // Hides Form1 but it is till in Memory
    }

Then when Form2 is Closed it will Unhide Form1.

   private void frm2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Show(); // Unhide Form1
    }

Upvotes: 1

Mark Hall
Mark Hall

Reputation: 54522

ShowDialog opens your secondary Form as Modal Dialog, meaning that the MainForm's code execution will stop at that point and your secondary Form will have the focus. so all that you need to do is put a this.Show after your ShowDialog call.

From above link:

You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.

private void countClick(object sender, EventArgs e)
{
    this.Hide(); 
    subForm myNewForm = new subForm();
    myNewForm.ShowDialog();
    this.Show();
}

Upvotes: 2

Chris
Chris

Reputation: 7174

You need to find some way to pass a reference to the main form to the second form click event handler.

You can do this either by setting the form as a member variable of the second form class or pass it via the event arguments.

Upvotes: 0

Related Questions