Best way to kill the form after the FormClosing event is overridden to hide rather than close?
I have a simple Windows Form that contains property controls at runtime. When the user clicks the Close [X] button, I want to keep the window and its contents alive, not kill it by handling the FormClosing event, canceling the event, and just hiding the form.
This is fine, but when I close the application, I need to close the window. I have implemented the below but it feels kludgey. Is there an easier, smarter way to handle this situation? (The form controller calls KillForm explicitly after it receives the close event from the main window.)
Friend Class HostForm
Private _hideInsteadOfClosing As Boolean = True
Private Sub HostForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
Handles Me.FormClosing
If _hideInsteadOfClosing Then
Me.Hide()
e.Cancel = True
End If
End Sub
Public Sub KillForm()
_hideInsteadOfClosing = False
Me.Close()
End Sub
End Class
source to share
You can check the value of the property of the CloseReason
args event object. If it is UserClosing
, hide the form, otherwise close it. For all possible values of this property check the documentation for the CloseReason enumeration .
source to share