New form fails to appear

37 views Asked by At

Can someone explain why my new form doesn't appear? The old form closes but it isn't replaced by my new one:

namespace Pong
{
    public partial class Menu : Form
    {
        public Menu()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void PlayButton_Click(object sender, EventArgs e)
        {
            PongForm form = new PongForm();
            form.Show();
            this.Close();
        }

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

There are 1 answers

0
Idle_Mind On

Is "Menu" the startup form for your application? If yes, then when it closes the entire application closes.

One solution would be to hide the current form, display "form" with ShowDialog(), then call Close():

    private void PlayButton_Click(object sender, EventArgs e)
    {
        this.Visible = false; // hide the current form
        Application.DoEvents();

        PongForm form = new PongForm();
        form.ShowDialog(); // code stops here until PongForm is dismissed

        this.Close(); // now close the form (and presumable the whole app)
    }