Reputation: 265
how can i make my Form not to close but to Hide when user click on the "X" button on the form?
As i have a NotifyIcon that contain an Exit menu which actually does the Form Closing (i don't want the Form's "X" to close the form but to hide it).
thanks.
Upvotes: 2
Views: 3675
Reputation: 4848
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
Me.Hide()
End Sub
Upvotes: 1
Reputation: 942020
Just implement the FormClosing event. Cancel the close and hide the form unless it was triggered by the notify icon's context menu. For example:
Private CloseAllowed As Boolean
Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
If Not CloseAllowed And e.CloseReason = CloseReason.UserClosing Then
Me.Hide()
e.Cancel = True
End If
MyBase.OnFormClosing(e)
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
CloseAllowed = True
Me.Close()
End Sub
Upvotes: 4
Reputation: 30883
You need to handle Form.Closing event and set e.Cancel to true to stop the form from closing. To hide it call Hide method.
Upvotes: 3