-2

I have no idea why this isn't working. Here's the code

private void button1_Click(object sender, EventArgs e)
{
    this.Close();
    new MainScreen().Show();
}

All that this is doing is closing the current form, it isn't opening the MainScreen Form. What am I doing wrong? I've used this same code in another project.

zx485
  • 28,498
  • 28
  • 50
  • 59
L P
  • 11
  • 1
  • 4
  • 4
    If you close MainForm your application will close. Please provide more details about your forms. Which one is executing this code, child or main form? – opewix Jan 20 '19 at 16:58
  • 3
    Possible duplicate of [opening a window form from another form programmatically](https://stackoverflow.com/questions/15041382/opening-a-window-form-from-another-form-programmatically) – Ňɏssa Pøngjǣrdenlarp Jan 20 '19 at 17:04
  • My crystal ball says that after the Close() call your app does not have any window left. The OS needs to find another window to put in the foreground. Bummer if that is a large window owned by a different process, like the VS main window. Your main window opens up behind it and you can't see it. Simply swap the two statements to fix. – Hans Passant Jan 20 '19 at 17:19

2 Answers2

0

This code worked in my program:

 this.Hide();
 this.Close();
 MainScreen mainScreen = new MainScreen();
 MainScreen.ShowDialog();
Ivan
  • 1
-1

Open the other form first. Then Hide current one:

private void button1_Click(object sender, EventArgs e)
{
    this.Hide();
    new MainScreen().Show();
}
Umid Kurbanov
  • 174
  • 1
  • 5