Question

I'm venturing into making my VB.NET application a little better to use by making some of the forms modeless.

I think I've figured out how to use dlg.Show() and dlg.Hide() instead of calling dlg.ShowDialog(). I have an instance of my modeless dialog in my main application form:

Public theModelessDialog As New dlgModeless

To fire up the modeless dialog I call

theModelessDialog.Show()

and within the OK and Cancel button handlers in dlgModeless I have

Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
    Me.DialogResult = System.Windows.Forms.DialogResult.OK
    Me.Hide()
End Sub

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

and that seems to work fine.

The "X" button in the upper right is getting me, though. When I close the form with that button, then try to reopen the form, I get

ObjectDisposedException was unhandled. Cannot access a disposed object.

I feel like I'm most of the way there but I can't figure out how to do either of the following:

  • Hide that "X" button
  • Catch the event so I don't dispose of the object (just treat it like I hit Cancel)

Any ideas?

The class of this dialog is System.Windows.Forms.Form.

Was it helpful?

Solution

Use Me.Close() to hide the form. To open it, use the following snippet:

If theModelessDialog.IsDisposed Then
    theModelessDialog = New dlgModeless
End If
dlgModeless.Show()

If this is saving data, then you'll need to figure some way of storing it (perhaps in a static variable/s in the form). This is the proper way to do what you are trying to achieve though.

You'll also have to forgive me if my VB is off, it's been a while.

OTHER TIPS

Catch the FormClosing event and, if the reason is UserClosing, set Cancel on the event to true.

Something like the following:

Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _ 
     Handles Form1.FormClosing

    if e.CloseReason = CloseReason.UserClosing then
        e.Cancel = true
        Me.Hide()
    end if

End Sub

the formclosing event allows me to do a managed exit of the form so I have included a question to confirm to exit. I also have a form flag bterminate to force the cancel where i want it to and therefore not ask the question. Thanks your suggestion helped me as well :)

    Dim msgboxresponse As MsgBoxResult

    If e.CloseReason = CloseReason.UserClosing Then
        If Not Me.bTerminate Then
            msgboxresponse = MsgBox("Are you sure you want to cancel adding?", _
                                MsgBoxStyle.Question + MsgBoxStyle.YesNo, Me.Text)
            If msgboxresponse <> MsgBoxResult.Yes Then
                e.Cancel = True
                Return
            End If
        End If
    End If

@John was Hiding the form in his code and the answers above provide a solution to that case. Often, though, you are not planning to use the form again, so you really do want the form to be Disposed. All Close related activities will be in one place if you Handle the FormClosing event using Me.FormClosing by adding it to anyCancel/Close/Exit code that you already have. e.g. in @John's case:

Private Sub Cancel_Button_Click(ByVal sender As System.Object, _ 
                                     ByVal e As System.EventArgs) _
                                 Handles Cancel_Button.Click, Me.FormClosing
....More code
Me.Dispose
End Sub

Note the use of the Me.Dispose instead of any existing Me.Close. If you leave the Me.Close you'll create an infinite loop. See this for the subtle differences between Close and Dispose.

Agree with handling the FormClosing event. Or change the properties on the form to hide the system X control.

I've tried everything and it didn't work if you just want to close, without showing an messagebox, you will just need:

Private Sub FORM1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing

 >e.Cancel = False
 >FORM2.Show()   (if you want to show another form)

End Sub

Hope this helps you...!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top