Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I control the behavior of the "X" close button in the upper right of a winform?

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.

like image 276
John Avatar asked May 15 '10 05:05

John


People also ask

How can I tell if clicked X or close button?

To detect if the user clicked either X or your CloseButton, you may get it through the sender object. Try to cast sender as a Button control, and verify perhaps for its name "CloseButton", for instance. private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (string. Equals((sender as Button).

How do you close a form in VB net?

If you want to close the form then use Me. Close() instead. The Load event will fire again when you create the new instance.

How to handle form closing event in c#?

When a form is closed, all resources created within the object are released and the form is disposed. If you cancel this event, the form remains opened. To cancel the closure of a form, set the Cancel property of the CancelEventArgs passed to your event handler to true .


4 Answers

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
like image 74
Michael Todd Avatar answered Oct 19 '22 01:10

Michael Todd


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.

like image 38
Matthew Scharley Avatar answered Oct 19 '22 03:10

Matthew Scharley


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
like image 24
Philip Avatar answered Oct 19 '22 01:10

Philip


@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.

like image 39
Neil Dunlop Avatar answered Oct 19 '22 02:10

Neil Dunlop