[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

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

0
KentDBerry
Asked:
KentDBerry
1 Solution
 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now