0

I have a C# application which makes use of different forms. I know how to switch between forms using the following example:

{
    Form1 login;
    login = new Form1();
    login.Show();
    this.Hide();  
}

One of my forms contains a web-browser. How do I switch between the web-browser form and another form without having to close the web-browser so that the session on the web-page is not closed?

I have tried simply hiding the form, but I do not know how to open it again without having to use the above mentioned code.

Please help!

ASh
  • 34,632
  • 9
  • 60
  • 82
Ewert
  • 1
  • 3
    Just do show on the form instance that you already have without a new. – Philip Stuyck Mar 18 '15 at 08:05
  • The simplest way is to in fact allow the window to close, that way you don't have to keep track of those awkward hidden windows. You just have to prevent your app from quitting when the startup window closes. [Like this](http://stackoverflow.com/a/10769349/17034). – Hans Passant Mar 18 '15 at 09:37

2 Answers2

0

Disclaimer: I am presuming that you have a button on your other form to show this first one again.

What you are probably missing is a reference to your first form in your webbrowsers form, which you can pass in the constructor.

Form1 myForm

public WebBrowserForm(Form1 myForm)
{
    myForm = myForm;
}

Then your button on this form can just show myForm again (myForm.Show() and hide if needed)

On your first form, you will need to move the variable login into the class so you can reference this again

Form1 login = new Form1();

{
    login.Show();
    this.Hide();
}
Sayse
  • 42,633
  • 14
  • 77
  • 146
0

Assuming WebBrowserForm as the name of your form containing the WebBrowser

if (Application.OpenForms["WebBrowserForm"] != null)
      (Application.OpenForms["WebBrowserForm"] as Form).Show();

This code checks for a WebBrowser Form that has intantiated before and shows it up if it is not null

Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45