I am using this code to minimise the form to the tray when the X button is pressed
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
NotifyIcon1.Visible = True
NotifyIcon1.Icon = SystemIcons.Application
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Running In the Background"
NotifyIcon1.BalloonTipText = "Application is running in the Background. Double click it to maximize the form"
NotifyIcon1.ShowBalloonTip(50000)
Me.Hide()
ShowInTaskbar = True
e.Cancel = True
End Sub
I have a Quit button too which will actually exit the application, but I think it's using the code above to minimise the form instead.
Private Sub btn_Quit_Click(sender As Object, e As EventArgs) Handles btn_Quit.Click
Dim confirm As DialogResult = MessageBox.Show("Are you sure you wish to Exit the App?", "Exit Application?", MessageBoxButtons.YesNo)
If confirm = DialogResult.Yes Then
Application.Exit()
End If
End Sub
How can I override the FormClosing
sub when using the Quit button?
I tried using End
but that didn't work either
CodePudding user response:
You should use the FormClosingEventArgs passed to Form_Closing handler. It will tell you whether someone tried to close the Form, or if the application is exiting. There are other reasons too which you can check out
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Select Case e.CloseReason
Case CloseReason.ApplicationExitCall
e.Cancel = False
Case CloseReason.FormOwnerClosing
NotifyIcon1.Visible = True
NotifyIcon1.Icon = SystemIcons.Application
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Running In the Background"
NotifyIcon1.BalloonTipText = "Application is running in the Background. Double click it to maximize the form"
NotifyIcon1.ShowBalloonTip(50000)
Me.Hide()
ShowInTaskbar = True
e.Cancel = True
End Select
End Sub