Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preventing 2 instances of the same form

Status
Not open for further replies.

levogiro

Programmer
Apr 30, 2001
50
BR
hi,

i'm developing an MDI application and i'd like to open just one instance of a form. i tried the code below and it works when the form is loaded for the first time, but if i close the form and try to open it again i receive this error message: CANNOT ACCESS A DISPOSED OBJECT NAMED "frmUsuario".

Code:
Dim fUsuario As frmUsuario

Private Sub mniUsuario_Click(ByVal sender As 
                             System.Object, 
                             ByVal e As System.EventArgs) 
            Handles mnuUsuario.Click

    If fUsuario Is Nothing Then
        fUsuario = New frmUsuario
    End If

    fUsuario.MdiParent = Me
    fUsuario.Show()

End Sub

thanks,

benedito.
 
I would do it another way.
this in the closing event of frmusuario

Code:
e.cancel = true
me.tag = "Hide" 
me.hide

and then replace your code with this.

Code:
Dim fUsuario As new frmUsuario

Private Sub mniUsuario_Click(ByVal sender As 
                             System.Object, 
                             ByVal e As System.EventArgs) 
            Handles mnuUsuario.Click

    If fUsuario.tag <> "Show" Then
        fUsuario.tag = "Show"
        fUsuario.MdiParent = Me
        fUsuario.Show()
    End If


End Sub

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
actually, i did this way...it's working, but i don't like to see my form wandering in the MDI form every time i load it...i'd like to know if there's a way to avoid this.

Code:
    Dim fUsuario As frmUsuario

    Private Sub mniUsuario_Click(ByVal sender As
                                 System.Object,
                                 ByVal e As 
                                 System.EventArgs)
                Handles mnuUsuario.Click

        If Not fUsuario Is Nothing Then
            fUsuario.Close()
        End If

        fUsuario = New frmUsuario
        fUsuario.MdiParent = Me
        fUsuario.Show()

    End Sub
 
JohnYingling,

i tried your code and it worked fine, thanks a lot...but everytime i load the form it appears at a different place in the MDI form. it there a way to avoid this ?

again, thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top