Question Prevent Duplicate forms

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
I have an menu form that is an mdi container. When my app opens, it opens up the menu form. It then does:

VB.NET:
        Dim newmdichild As New frm_login
        newmdichild.MdiParent = Me
        newmdichild.Show()

        newmdichild.Left = (Me.Width / 2) - (newmdichild.Width / 2)
        newmdichild.Top = (Me.Height / 2.5) - (newmdichild.Height / 2)

which opens the login form. After login, the login form closes. On the main menu if they logout, the login button is re enabled. I want to do a check to make sure it isnt already open. This isnt just for this form, but others. I am trying to prevent multiple instances of forms from opening. I search the internet and found this:

VB.NET:
    Public Shared Function ChildFormOpen(ByRef ParentForm As Form, ByRef Childform As Form) As Boolean
        ' Used to find out if a child form is open in the parent container

        ' Usage:
        ' dim secForm as new secondform
        ' if childformopen(parentform,childform) then
        ' secform.activate
        ' else
        ' secform.mdiparent = me
        ' secform.show
        ' end if
        ' secform = nothing

        Dim blnIsOpen As Boolean = False
        Dim frm As Form
        For Each frm In ParentForm.MdiChildren
            If Childform.Name = frm.Name Then
                Childform.Focus()
                blnIsOpen = True
                Childform = frm
                Exit For
            End If
        Next
        ChildFormOpen = blnIsOpen
        frm = Nothing
    End Function

So when I try to use this:
VB.NET:
    Private Sub LogonBBI_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles LogonBBI.ItemClick
        Call DisableMenu()
        If ChildFormOpen(Me, frm_login) Then
            Dim newmdichild As New frm_login
            newmdichild.MdiParent = Me
            newmdichild.Show()

            newmdichild.Left = (Me.Width / 2) - (newmdichild.Width / 2)
            newmdichild.Top = (Me.Height / 2.5) - (newmdichild.Height / 2)
        End If
    End Sub

I get an error if the form is already open. On the if line, I get property can only be set to nothing. What am I doing wrong? How do I fix this?

I read something about default instances, but don't completely understand it. I thought that is what I was doing with Dim newmdichild As New frm_login, but i guess not.

Any help would be appreciated.

Oh and I am using VS 2010
 
I read something about default instances, but don't completely understand it. I thought that is what I was doing with Dim newmdichild As New frm_login, but i guess not.
No, you're creating new instances, to use the default form instance just use the type name as if it already were an object, for example:
frm_login.Show()
 
jmci, i actually did read through that, couldnt really follow it. I think I figured it out thought.

JohnH, I know i can do that, but what if it is an mdi child?
 
JohnH, I know i can do that,
In previous post you said you didn't know that. Good to hear that now you do know that, glad I could help.
but what if it is an mdi child?
So what? A MDI child is just a form that has a property set. A form is a form, and each form has a default instance.
 
Back
Top