4

I have a Form1 and another one that I added. Form1 is being run by program.cs at the start. I need to hide Form1 and show options form by the press of a button.

    private void submitPassword_Click(object sender, EventArgs e)
    {
        options optionForm = new options();
        optionForm.Show();
    }

the above code opens the options form on top, but I need it to replace the current form. how can I achieve this?

Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65

4 Answers4

9

Hide current form using this.Close() before showing new one and make sure you are using parameterless Application.Run so program won't close when you close it's main form.

MagnatLU
  • 5,967
  • 1
  • 22
  • 17
  • how can I use parameterless Application.Run ?? my current code that runs the form is : `Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1());` – Ahoura Ghotbi Nov 26 '11 at 17:37
  • 3
    Replace `Application.Run(new Form1());` with `new Form1().Show(); Application.Run();`. – MagnatLU Nov 26 '11 at 17:39
  • thanks alot, it works perfect... if you dont mind can you answer my last question. how can I have a main form and load other forms inside it?? – Ahoura Ghotbi Nov 26 '11 at 18:11
  • If you want your other forms literally inside main form, set `IsMdiContainer` on your main form and set `MDIParent` on child forms to your MDI Container (main form) before showing them. – MagnatLU Nov 26 '11 at 18:17
  • Thanks, that helped. Was struggling with this for an hour before I found this answer. – Najeeb Jun 29 '18 at 10:14
2

You can use "Hide" and "Show" method :

private void submitPassword_Click(object sender, EventArgs e)

{

    options optionForm = new options();

    this.Hide();

    optionForm.Show();
}
saeid1389
  • 25
  • 1
  • 6
2
private void submitPassword_Click(object sender, EventArgs e)
{
    options optionForm = new options();
    optionForm.Show();
    this.Hide();
}
Seth
  • 6,514
  • 5
  • 49
  • 58
Vero009
  • 612
  • 3
  • 18
  • 31
1

Similar solutions where one form calls and acts on another... Such as this one I answered for another. You could do a similar process... pass in your first form to the second... Then show the second... Then, you could HIDE your first form (via this.Hide() ). Then, in your second form, when you click whatever button to select your choice, and need to return back to the first form, you could then use the original form's reference passed INTO the second form to re-Show it, such as in the click on the second form...

this.PreservedForm.Show();  // re-show original form
this.Close();   // and CLOSE this second form...
Community
  • 1
  • 1
DRapp
  • 47,638
  • 12
  • 72
  • 142