开发者

How to Clicking "X" in Form, will not close it but hide it

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜