Click here to Skip to main content
15,914,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm wanting to check to see how many times a particular form is opened in my project, so that only one form is open.
Posted
Updated 27-Jan-11 3:47am
v2

Why not just create your form as a member variable, and thus you'll only ever be able to show one ? If you want to track how many, just put a static variable in the form that gets incremented. The lack of deterministic destruction means it's most reliable to make it go up and down by hand, although there's probably an event you can hook in to to do it for you, still, such as when the forms visible status changes to not visible.
 
Share this answer
 
Adding to CG's response
---------------------------

One popular approach is to hook the Closed event of the form in the parent form, so that when the child form's closed, the member variable can be set to null. Thus if the member variable is null, it means the form needs to be recreated and reshown, else it means the form's already visible and probably just needs to be focused/activated.
 
Share this answer
 
v2
Comments
Espen Harlinn 27-Jan-11 17:26pm    
5+ either that or just hide the form on close, and reuse the instance
Nish Nishant 27-Jan-11 18:50pm    
Yeah, that'd work too.
See this link about singletons. I have used this pattern in the past to ensure I only have one instance of a form.

http://msdn.microsoft.com/en-us/library/ff650316.aspx[^]
 
Share this answer
 
If you're looking for a solution that will make sure that only one instance of a form is ever opened please have a look at my answer to this question (Answer 2): How to open only one instance of class not more than that?[^]
It handles a case were only one instance of a form is ever to be opened while the application is running.

Hope that helps you!
 
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