Results 1 to 7 of 7

Thread: (Resolved) Check if form is loaded before starting another instance

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    (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:
    1. '
    2.         Dim aForm As New frmCSA
    3.         If aForm Is Nothing Then
    4.             aForm.MdiParent = Me
    5.             aForm.Show()
    6.         Else
    7.             aForm.BringToFront()
    8.         End If
    Last edited by FastEddie; May 20th, 2009 at 08:23 AM.

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    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
  •  



Click Here to Expand Forum to Full Width