ObjectDisposedException error

On my main form, I have a menu item that calls the below code.  In the Sub New() of the form, I call a login form that determines permissions of user on that form.  However, if the user presses the cancel button on the login form, the login form will close and so will the below frm.  And I get an  ObjectDisposedException error on the line fmr.ShowDialog().  (i.e. Cannot access a disposed object).

What do I need to to do to work around this errror.  I was hoping the Try Catch statement would trap the error but it does not.
Private Sub UserMaintenanceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UserMaintenanceToolStripMenuItem.Click
        Try
            Dim frm As frmUsersMaintenance = New frmUsersMaintenance
            frm.ShowDialog()
        Catch ex As Exception
        End Try
 
    End Sub

Open in new window

KentDBerryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulHewsCommented:
The code for the login form should return a dialog result from the cancel button:

Public Class Form2

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
        'Other code that validates login, etc.
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class

Then you can call it like this:

       Dim frm As New Form2
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            MsgBox("okay")
        Else
            MsgBox("canceled")
        End If
        frm.Dispose()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zer0_ringCommented:
Are you disposing the form that this menu item is part of?
It seems to me that you may need to work out your events esp. if your calling form Close event before doing other things.
0
KentDBerryAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.