Keeping track of a form
-
I have a form and I want to make it so only one instance of that form is opened. I can do this with a counter variable in my form class so when it is loaded it incremenets and if they go to file->exit it decriments. But if thye hit the X button in the upper right, the form is closed and my counter vraiable stays at one. I need a way to make it so however they close it my counter is updated accordingly.
-
I have a form and I want to make it so only one instance of that form is opened. I can do this with a counter variable in my form class so when it is loaded it incremenets and if they go to file->exit it decriments. But if thye hit the X button in the upper right, the form is closed and my counter vraiable stays at one. I need a way to make it so however they close it my counter is updated accordingly.
I would suggest that you use the Singleton pattern instead of tracking counters.
-
I would suggest that you use the Singleton pattern instead of tracking counters.
-
the following is similar to how the vb6 upgrade wizard does it. it's not a true singleton pattern, because it allows for more than one to be created over the lifetime of the app, but is close enough in that it never allows more than one at a time. add this code to your form, substituting "Form1" for the type of your form. Private Shared m_SingleInstance As Form1 Private Shared m_InitializingSingleInstance As Boolean Public Shared Property SingleInstance() As Form1 Get If m_SingleInstance Is Nothing OrElse m_SingleInstance.IsDisposed Then m_InitializingSingleInstance = True m_SingleInstance = New Form1() m_InitializingSingleInstance = False End If SingleInstance= m_SingleInstance End Get End Property now when you want to create or reference your one form, use Form1.SingleInstance instead of creating or showing it manually.
-
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp There are a couple of others there too: factory and .. er.. I forgot. Alice thought that running very fast for a long time would get you to somewhere else. " A very slow kind of country!" said the queen. "Now, here , you see, it takes all the running you can do, to keep in the same place".
-
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp There are a couple of others there too: factory and .. er.. I forgot. Alice thought that running very fast for a long time would get you to somewhere else. " A very slow kind of country!" said the queen. "Now, here , you see, it takes all the running you can do, to keep in the same place".
Just curious - why did you change your nick to jkgh? "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied." - Alex, 13 June 2002 Please review the Legal Disclaimer in my bio.