C# C# form open and close problem?

  • Thread starter Thread starter rollcast
  • Start date Start date
  • Tags Tags
    Form
Click For Summary
The discussion centers on creating a splash screen using two forms in a program. The user aims to display Form1 for three seconds upon startup before transitioning to Form2. The provided code attempts to implement this but encounters issues due to creating a new instance of Form1 in Form2, which does not close the original instance. The suggestion is to modify the approach by having Form1 close itself or allowing Form2 to manage the transition. This indicates that the user is essentially trying to build a splash screen, which could guide further research for effective implementation.
rollcast
Messages
403
Reaction score
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
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
12
Views
28K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
8
Views
2K
Replies
1
Views
2K
Replies
3
Views
3K