Click here to Skip to main content
15,564,350 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello sir/Mam

I have created 2 forms in c#.

I want to close 2nd form (not existing) from 1st form in c#.

Below code is not working.

2nd_form 2n = new 2nd_form();
2n.close();

Please help.

What I have tried:

Below code is not working.

2nd_form 2n = new 2nd_form();
2n.close();

Please help.
Posted
Updated 19-Mar-19 0:31am

You have to show a form first before it can be closed.
You can try this:
2nd_form 2n = new 2nd_form();
2n.Show();
// Your code here e.g. Thread.Sleep(5000)
// or put a breakpoint on the next line
2n.Close();
 
Share this answer
 
It cant work: you are closing an instance of the form that you just created - that's what new means!

Think of it this way: If you put your phone in the glove box of your car and then buy a new car would you expect to open the new glove box and find your phone there? Of course not - the glove box is part of the old instance of a Car and that is entirely separate from the new Car.

To close the version the user is looking at you need to locate it - which means looking at your code where you Show the form and working out where you have stored the instance.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900