|
-
May 19th, 2009, 04:13 PM
#1
Thread Starter
Hyperactive Member
(Resolved) Check if form is loaded before starting another instance
I have a Parent form that loads a child form and I only want to allow one instance of the child form. I have this on the button to open the form. Stepping through the code with the debugger it "appears" to work OK when the form is open (I say appears because nothing changes) but when the form is closed it doesn't open as I would expect. What am I missing?
VB Code:
'
Dim aForm As New frmCSA
If aForm Is Nothing Then
aForm.MdiParent = Me
aForm.Show()
Else
aForm.BringToFront()
End If
Last edited by FastEddie; May 20th, 2009 at 08:23 AM.
-
May 19th, 2009, 04:24 PM
#2
Re: Check if form is loaded before starting another instance
I wouldn't have thought that aForm would ever be nothing as you are declaring it as New.
You could try this :
Code:
Private Function DoesFormExist() As Boolean
Dim Exists As Boolean = False
For Each TestForm In Me.MdiChildren
If TypeOf (TestForm) Is frmCSA Then
Exists = True
Exit For
End If
Next
Return Exists
End Function
-
May 19th, 2009, 05:25 PM
#3
Re: Check if form is loaded before starting another instance
If you only want a single instance of a form, have you considered a Singleton pattern? That may be more extreme than you want, as that will mean that there will only be one form for the life of the program, but it's practically what you are doing.
To make a Singleton, add a constructor and declare it as Private. This will mean that NO code other than the form class can call the constructor. Since nothing but the class can call the constructor, you would also need a way for the class to create itself, and a sub like this would do:
Code:
Private Shared myForm as <your form type here>
Public Shared Function GetForm() as <your form type here>
If myForm Is Nothing Then
myForm = New <your form type here>
End If
Return myForm
End Function
Since the function is Shared, it is created when the program starts, and isn't tied to an instance of the form, but since it is a member of the form, it can access private class members, such as the constructor.
Using this design, you would get rid of the code to create an instance of the form and just call:
<your form type>.GetForm.Show
and so on.
My usual boring signature: Nothing
 
-
May 19th, 2009, 10:38 PM
#4
Re: Check if form is loaded before starting another instance
Shaggy, you should modify your code like this:
Code:
If myForm Is Nothing OrElse myForm.IsDisposed Then
It's also worth noting that, while I'm not a fan of them, default instances provide a pseudo-singleton pattern too. They don't prevent you creating additional instances but, if you always refer to the default instance, that's the only instance you'll ever see. You can just do this:
Code:
Form1.Show()
Form1.Activate()
If the form has never been displayed then the first line will create it and display it and the second line has no effect. If the form has been displayed and closed then the first line will create a new instance and display it and the second line has no effect. If the form is already being displayed then the first line has no effect and the second line will make sure it has focus. The code to use a form that implements the Singleton pattern would be basically the same:
Code:
Form1.GetForm().Show()
Form1.GetForm().Activate()
so this is probably the most legitimate use of default instances.
Last edited by jmcilhinney; May 19th, 2009 at 10:43 PM.
-
May 20th, 2009, 07:32 AM
#5
Thread Starter
Hyperactive Member
Re: Check if form is loaded before starting another instance
I was having a little trouble with the Singleton pattern but I was able to get the sample by Keystone Paul to do what I need. My only "issue" with that function is that I would like to make it generic so that I can pass the form name in as an argument. I did do some experimenting with it but I couldn't get the syntax correct.
-
May 20th, 2009, 07:39 AM
#6
Re: Check if form is loaded before starting another instance
Using string names is best avoided if possible, which in this case it is. Check this out:
http://www.vbforums.com/showthread.php?t=487332
-
May 20th, 2009, 08:22 AM
#7
Thread Starter
Hyperactive Member
Re: Check if form is loaded before starting another instance
jmcilhinney that is perfect. Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|