C# form open and close problem?

  • C#
  • Thread starter rollcast
  • Start date
  • Tags
    Form
In summary, the conversation discusses the need to have a form (form1) appear when the program is started, stay on screen for 3 seconds, and then close, followed by the opening of another form (form2). The code provided shows attempts at achieving this, but it seems there may be a coding error as the desired outcome is not being achieved. The suggestion is made to research building a splash screen as it may provide helpful information.
  • #1
rollcast
408
0
I'm trying to make a form, form1, appear when the program is started, stay on screen for 3 seconds and then close. Form2 should then open after this.

I tried various ways of doing it but I can't get it to work as I want it to do so. There are no bugs in the code so its purely a coding error.

Code for form1

Code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            waitandopen();
        }

        private void waitandopen()
        {
            System.Threading.Thread.Sleep(3000);

            Form form2 = new Form2();
            form2.Show();
        }
    }
}

Code for form 2

Code:
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            closeform1();
        }

        private void closeform1()
        {
            Form form1 = new Form1();
            form1.Close();
        }
    }
}
 
Technology news on Phys.org
  • #2
Well, for one, in Form2 you are creating a new instance of Form1 and then closing it, instead of the instance you already have. You can have Form1 close itself, or have Form2 open it.

But frankly, it looks like you are trying to build a splash screen - maybe that will give you a handle to search for more information.
 

Related to C# form open and close problem?

1. How can I open a C# form?

To open a C# form, you can use the Show() method, passing in the form you want to open. For example: Form1.Show();

2. How can I close a C# form?

To close a C# form, you can use the Close() method, which will close the current form. Alternatively, you can use the Dispose() method, which will also release any resources used by the form.

3. Why is my C# form not closing?

There are a few possible reasons why your C# form may not be closing. One reason could be that you have not called the Close() method on the form. Another reason could be that there are still event handlers or other references to the form that are preventing it from being closed. You can use the debugger to check for any active references to the form.

4. How can I prevent my C# form from closing?

To prevent a C# form from closing, you can use the FormClosing event. In the event handler, you can set the Cancel property to true to cancel the form closing. Alternatively, you can override the OnFormClosing method and set e.Cancel = true; to prevent the form from closing.

5. How can I detect when a C# form is closed?

You can detect when a C# form is closed by handling the FormClosed event. This event is raised after the form is closed and all resources have been released. You can also check the IsDisposed property of the form, which will be true after the form is closed and disposed.

Similar threads

  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
17
Views
3K
  • Programming and Computer Science
Replies
12
Views
27K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
Back
Top