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 th开发者_C百科e Form Closing (i don't want the Form's "X" to close the form but to hide it).
thanks.
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
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.
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
精彩评论