Search code examples
c#winformsscene

Scenes aren't switching properly


so I've got two scenes in a Windows Forms Application, and they're supposed to switch. However, it's not working correctly. Here's the code for Form1:

using System;
using System.Windows.Forms;

namespace Chat_Room {
    public partial class SceneOne : Form {
        public SceneOne() {
            InitializeComponent();
        }
        private void createRoomButton_Click(object sender, EventArgs e) {
            Form2 scene = new Form2();
            scene.Show();
            this.Close();
        }
    }
}

I know, this is a small question, but I had had all the code for both in this one form, and then I decided to split it up into two, but then once I moved the code, it stopped switching (now it just closes the first one). And yes, the second form is Form2.


Solution

  • From msdn - Form.Close:

    When a form is closed, all resources created within the object are closed and the form is disposed

    So, when you call this.Close(), scene is disposed too as it is "created within the object".

    As per this discussion, maybe you should try something like this:

    this.Hide();
    Form2 f = new Form2();
    f.Show();