Skip Me.FormClosing - vb.net

i've set a warning message before closing the form, but is there a way to skip it sometimes?
My code:
Sub Me_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Are you sure you want to cancel the installation?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
e.Cancel = False
Else
e.Cancel = True
End If
End Sub
but i have a final code that must close the app without this message:
Private Sub Done_Click(sender As Object, e As EventArgs) Handles Done.Click
'need to close without warning
Close()
End Sub
can you help me change this or add something that allows that button to close the form without launching Me.FormClosing?

Use a Boolean flag to determine the installation status (success or fail/abort)
Private installSuccess As Boolean ' False by default.
Private Sub Install()
Try
' Installer logic here
' ...
Me.installSuccess = True
Catch ' ex As Exception
End Try
End Sub
Then:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) _
Handles MyBase.FormClosing
If Me.installSuccess Then
Exit Sub
End If
If MessageBox.Show("Are you sure you want to cancel the installation?", "Warning",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) = DialogResult.Yes Then
e.Cancel = False
Else
e.Cancel = True
End If
End Sub

Related

Application.Exit() not working when using form closing methods to minimise to tray

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
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

vb.net formclosing with dialog

I can not believe I am asking this question after reading similar questions mostly OLD and here is the frustration I had code that worked just as desired then I changed to Option Strict ON now more issues than I bargained for
Here is the code that works OR I should say worked at one time
In fact it still works on another test project?
The Handles Me.Closing has a RED ERROR squiggle line under it
Private Sub frmTwo_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
Const message As String = "YES Exit Program" + vbCrLf + vbNewLine + "NO Back to Form Start"
Const caption As String = "Exit OR Cancel"
Dim result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
Application.Exit()
ElseIf result = DialogResult.No Then
frmStart.Show()
Me.Hide()
'tbMessage.Text = "CANCEL"
e.Cancel = True
End If
End Sub
Tested this code on two forms frmStart and frmTwo on frmTwo it does not stop Debugging
Private Sub frmStart_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Are you sure to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
e.Cancel = False
Else
Me.Hide()
frmTwo.Show()
e.Cancel = True
End If
End Sub
It seems with Option Strict ON VB.Net does not like constructing the Sub this way
Private Sub frmStart_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
And as mentioned you then need to include Imports System.ComponentModel
So to STOP playing e.Cancel = True and e.Cancel = False just write a Sub as seen below and Call that Sub from some Button no need for Imports System.ComponentModel
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
CloseForm()
End Sub
Public Sub CloseForm()
Const message As String = "YES Exit Program" + vbCrLf + vbNewLine + "NO Return to Form Start"
Const caption As String = "Exit OR Return"
Dim result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
Application.Exit()
ElseIf result = DialogResult.No Then
Close()
frmStart.Show()
End If
End Sub
Recreating the closing event, the event parameters are (sender As Object, e As CancelEventArgs) not the different types. I'm testing this against .Net Framework 4.8, what .Net are you targeting?
This worked as expected:
Private Sub frmStart_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
Const message As String = "YES Exit Program" + vbCrLf + vbNewLine + "NO CANCEL"
Const caption As String = "Exit OR Cancel"
Dim result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.No Then
Me.Text = "CANCEL"
e.Cancel = True
End If
End Sub

Alternatives for 'Handles Me.FormClosing' in modules

I would like to know of any alternatives to the 'Handles Me.FormClosing' in modules.
I have created code that will display a confirmation message upon clicking the 'X' button. The issue is, I need to put this code into a module to use on multiple forms where i can call it, however, when I attempt to do this the 'Handles Me.FormClosing' will not work.
Here is the code i am using:
Private Sub Close(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
FrmLogin.Close()
Else
e.Cancel = True
End If
End Sub
Whenever you create a new form:
Dim newForm as Form = New YourFormClass()
AddHandler newForm.FormClosing, AddressOf YourModule.Close
This will route all closing events you want through that sub. Then just remove the Handles Me.Closing unless there is something you aren't showing us that makes it relevant.
Try This. I made changes to work as you want :
Module YourModule
Public Function AskFormClosing() As Boolean
Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
Return True
Else
Return False
End If
End Function
End Module
And then
Private Sub Close(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If YourModule.AskFormClosing Then
Application.Exit
Else
e.Cancel = True
End If
End Sub
One possibility ist to create a function in your module which shows the MessageBox and exits Application if "Yes" is clicked else returns False.
Module YourModule
Private dontAskAgain As Boolean
Public Function AskFormClosing() As Boolean
If dontAskAgain = False Then
Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
dontAskAgain = True
Application.Exit()
End If
End If
Return dontAskAgain
End Function
End Module
And then you only need to set e.Cancel to the inverted result of the function.
Private Sub Close(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = Not YourModule.AskFormClosing
End Sub
If you prefer to use AddHandler as suggested by other people, you could use the following method that will lead to the same result.
Module YourModule
Public Sub AskFormClosing(sender As Object, e As FormClosingEventArgs)
If dontAskAgain = False Then
Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
dontAskAgain = True
Application.Exit()
Else
e.Cancel = True
End If
End If
End Sub
End Module
And then add the Handler like this:
Dim newForm as Form = New YourFormClass()
AddHandler newForm.FormClosing, AddressOf YourModule.AskFormClosing
But for your main-Form you will need to add the Handler in the Loading Event, like this:
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Me.FormClosing, AddressOf YourModule.AskFormClosing
End Sub
Rather than placing this code in a module for reuse, consider create a base Form that has the behaviour you wish to share and then have you other forms inherit from this base Form (rather than from System.Windows.Forms.Form).
See How to: Inherit Windows Forms on the Microsoft website.

How to close first form?

I have 2 forms in my project. Users cannot exit from the first from, frmOptometry. I have this implemented this way:
Private Sub frmOptometry_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
MessageBox.Show("You can only close the application from the receipt screen!")
e.Cancel = True
End Sub
The second form, frmReceipt can be exited from. I have it implemented this way:
Private Sub frmReceipt_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Are you sure you want to quit?",
"Obi-Wan Optometry Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation) =
DialogResult.No Then
e.Cancel = True
Else
frmOptometry.Close()
End If
End Sub
Right now, it does not close the program completely because it goes to the frmOptometry_FormClosing and prevents it. What should I do to fix this? Is there anyway to figure out where the user is trying to shut down the program and then decide from there?
One way to solve this is by introducing a flag (say, called FromReceipt) which indicates who asks the frmOptometry to close itself.
Something like this on frmOptometry will do:
Public FromReceipt As Boolean = False 'Add this
Private Sub frmOptometry_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not FromReceipt Then 'add this condition
MessageBox.Show("You can only close the application from the receipt screen!")
e.Cancel = True
End If
End Sub
And then on the frmReceipt:
Private Sub frmReceipt_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If MessageBox.Show("Are you sure you want to quit?",
"Obi-Wan Optometry Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation) =
DialogResult.No Then
e.Cancel = True
Else
frmOptometry.FromReceipt = True 'Add this line to tell the frmOptometry: this time you can really close it
frmOptometry.Close()
End If
End Sub

windows form not closing when message box is yes

This is my code:
Private Sub frmName_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
If MessageBox.Show("Are you sure you want to logout?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()
Else
e.Cancel = True
End If
End If
End Sub
If I click no it will cancel the form closing, but when I click yes the message box will appear repeatedly. What i want to happen is when I click the close button, and clicked yes it will close the form. How can I fix this?
As the Form is already closing, there is no need to call Close() again. The following code should work:
Private Sub frmName_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
If MessageBox.Show("Are you sure you want to logout?", _
"Exit", MessageBoxButtons.YesNo, _
MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
End If
End Sub