Making new forms not have multiples open

DanielB33

Active member
Joined
Mar 29, 2013
Messages
40
Programming Experience
1-3
I have 5 or 6 forms in a program. Currently, each time the user opens a form, another copy of the same form is opened even if one copy is currently open. This cannot happen in my program and simply makes the program looks sloppy. How can I prevent this?
Thx
 
Do you want to allow the user to open more than one different form (not another copy) at the same time? If not, displaying the forms as modal would require them to close each form before a different one can be opened.

The other option would be to simply have some flags in your program that are toggled when a form is open/closed. When a user tries to open a form, you could check the flag for that form to determine whether it is already open or not.

Hope this helps!
 
While I'm not a big fan of default instances, this is one situation where they can make things easier. Normally, you create and display a form like this:
Dim f1 As New Form1

f1.Show()
Instead of creating an instance yourself, you can just use the default instance, which is managed by the system:
Form1.Show()
The first time you use the default instance, the system will create it for you. The next time you use the default instance, you will use that same instance if it still exists or, if it has been disposed, the system will create a new one. All that is transparent to you. That means that you can use that second code and it will not create a new instance if one is already open. If you use this code:
Form1.Show()
Form1.Activate()
That will ensure that there is never more than one instance of Form1 open at a time. When you run that code, a new instance will be opened if there isn't one already and, if there is one open, it will receive focus.
 
Back
Top