开发者

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

开发者 https://www.devze.com 2023-01-07 12:54 出处:网络
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

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

精彩评论

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