Cancel form closing by user selection - vb.net

In a vb.net windows application, I need user to confirm before closing application. I have this code in FormClosing event
If BackgroundWorker1.IsBusy Then
Dim UserSelection As Integer = MsgBox("Do you want Cancel Processing and Exit Application?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Exit Application")
If UserSelection = 6 Then
BackgroundWorker1.CancelAsync()
e.Cancel = True
Else
????
End If
End If
How can I cancel form closing if user clicked No?
Tried e.Cancel = false but it didn't work (exits application).

e.Cancel = True would stop the form closing.

According to documentation "e.Cancel = True" PREVENTS the form from closing

This is full code for cancel form closed. We should use FormClosing Event.
Private Sub Frm1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Do you want to closed", Me.Text, MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then
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

How to prevent a form from closing in btnClose_Click

I am trying to prevent the user from closing the form when the DialogResult.No is True in vb.net. I also tried e.cancel=true but it does not work in btnClose_click. I mentioned that is not FormClosingEventArgs. I want put it in btnClose_Click.
Code copied from OP comment;
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
If CustomMessageBox.Show("Are You Sure?", Buttons.YesNo, Icons.Question,
AnimateStyle.ZoomIn) = Windows.Forms.DialogResult.Yes Then
If Windows.Forms.DialogResult.Yes Then
' ---Close The Form
End If
ElseIf Windows.Forms.DialogResult.No Or Windows.Forms.DialogResult.None Then
' ---Not Closing the Form
End If
End Sub
You need to use the FormClosing Event (or Closing befoce NET 2.0). This Event occurs before the close event and can be canceled.
Private Sub Form1_Closing(sender As Object, e As FormClosingEventArgs ) Handles Me.FormClosing
If CustomMessageBox.Show("Are You Sure?", Buttons.YesNo, Icons.Question,
AnimateStyle.ZoomIn) = Windows.Forms.DialogResult.Yes Then
e.Cancel = true ' <<<< close event will not occur, form stays open
End If
End Sub
FormClosing Event

Close Application with FormClosing Event VB.net

I try to exit my application with form closing event but confirmation message box appears twice.
This is what I have:
Private Sub FrmMainPlatform_FormClosing(sender As Object, e As FormClosingEventArgs) _
Handles MyClass.FormClosing
Dim result As Integer
result = MessageBox.Show("Are you want to close", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.None)
If (result = DialogResult.No) Then
e.Cancel = True
Else
Application.Exit()
End If
End Sub
I also tried with this solution:
Private Sub FrmMainPlatform_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
'nothing to do here the form is already closing
Case Windows.Forms.DialogResult.No
e.Cancel = True 'cancel the form closing event
'minimize to tray/hide etc here
End Select
End Sub
The form is closed but the application i still running.
#karihalan, I believe, you first need to make sure that the Form1 is actually the startup form of your application. You can confirm this from Project's properties. If so, then you don't even need to call Application.Exit().
Second, try replacing Me.FormClosing with MyBase.FormClosing... Like so:
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Also, make sure you are not subscribing to form closing event twice, maybe using Addhandler statement.
Hope this would help.
UPDATE: You can try this to close all forms. I would put it in the main formclosing event.
For each f as Form in My.Application.OpenForms
f.Close()
Next
Get rid of that first codeblock. This is what I did and it asked my if I wanted to close only once and it closed when I clicked yes and didn't when I said no.
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
'nothing to do here the form is already closing
Case Windows.Forms.DialogResult.No
e.Cancel = True 'cancel the form closing event
'minimize to tray/hide etc here
End Select
End Sub
Private Sub FrmMainPlatform_FormClosing(sender As Object, e As FormClosingEventArgs) _
Handles Me.Closing
Dim result As Integer
result = MessageBox.Show("Are you want to close", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.None)
If (result = DialogResult.No) Then
e.Cancel = True
Else
Application.Exit()
End If
End Sub
Hi,
I got temporary solution for this issue. The Exit method does not raise the Closed and Closing events, which are obsolete as of .NET Framework 2.0
Perhaps your application has more than one form, whenever you apply a closing event on a startup form it will be repeated as much as the number of forms you have. I have faced the same issue and got it solved by changing the event handles on the other forms Not the startup one just like the example below:
From:
Handles Me.Closing
To:
Handles MyBase.Closing

VB.Net program minimized not working

I have a code like below
Private Sub InterfaceProg_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
'program minimized
Try
If Me.WindowState = FormWindowState.Minimized Then
Me.Visible = True
NotifyIcon1.Visible = True
NotifyIcon1.ShowBalloonTip(1, "Browser Bandwidth Optimizer", "Program Minimized", ToolTipIcon.Info)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
The code is working fine but there is one problem.
When i press the minimize button on top right, the program go to system tray
When i press close button, the program also go to system also go to system tray.
i want to make the program go to system tray if user press close button only and minimized the program to taskbar if user press minimized. how to do it?
Use the FormClosing event instead of the Resize event:
Private CloseAllowed As Boolean
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not CloseAllowed And e.CloseReason <> CloseReason.WindowsShutDown Then
Me.Hide()
e.Cancel = True
NotifyIcon1.Visible = True
'' etc..
End If
End Sub
You still need to give the user a way to exit the program. The context menu for the NotifyIcon is the usual approach. Add an Exit item:
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
CloseAllowed = True
Me.Close()
End Sub
When ever you press the minimize or close buttons a WM_SYSCOMMAND message is sent. The WPARAM specifies which button is pressed:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = &H112 Then 'WM_SYSCOMMAND
If CInt(m.WParam) = &HF060 Then 'SC_CLOSE, the close button is pressed
Me.Visible = False
Me.ShowInTaskbar = False
Return 'cancel the message
End If
If CInt(m.WParam) = &HF020 Then 'SC_MINIMIZE, the minimize button is pressed
'do your staff
End If
End If
MyBase.WndProc(m)
End Sub
WM_SYSCOMMAND message
Then you need to call your Code of "InterfaceProg_Resize" in the form closing even.
So make a new sub "Private Sub ProgToTray()"
Also you don't need a try catch for it.
You also need to hide the Taskbar Icon "Me.ShowInTaskbar = False"
And minimize the form"Me.WindowState = FormWindowState.Minimized"
Private Sub ProgToTray()
Me.ShowInTaskbar = False
Me.WindowState = FormWindowState.Minimized
NotifyIcon1.Visible = True
NotifyIcon1.ShowBalloonTip(1, "Browser Bandwidth Optimizer", "Program Minimized", ToolTipIcon.Info)
End Sub
If the user clicks x on your form ,Then you need to Cancel the closing of the form in the FormClosing event."e.Cancel = True"
Then you call "ProgToTray()"
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
e.Cancel = True
ProgToTray()
End Sub
Then if you close the form with x it will go to the notify area.
Don't forget to make a click event to open the form again from the notify icon and an option so the user can exit the form.
To make an exit button you need to add a ContextMenuStrip1 to your form .
To open the ContextMenuStrip1 when you right click the NotifyIcon1 you need add the code "ContextMenuStrip1.Show(Cursor.Position)" to you NotifyIcon1 click event
You also need to check which button is clicked.
"If e.Button = Windows.Forms.MouseButtons.Right Then" for the right button.
and
"e.Button = Windows.Forms.MouseButtons.Left Then" for the left button.
So if the user clicks left the form will open again and when he clicks right the ContextMenuStrip1 will show.
If the user clicks left mouse button you set the formwindowstate back to normal.
Also show the taskbar icon again and hide the tray icon.
Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(Cursor.Position)
ElseIf e.Button = Windows.Forms.MouseButtons.Left Then
Me.WindowState = FormWindowState.Normal
Me.ShowInTaskbar = True
NotifyIcon1.Visible = False
End If
End Sub
Then you make an click event for the exit button and remove the handler for the form closing event "RemoveHandler MyBase.FormClosing, AddressOf Form1_FormClosing".
The canceling of closing the form will not take place.
Then call me.close()
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
RemoveHandler MyBase.FormClosing, AddressOf Form1_FormClosing
Me.Close()
End Sub

How to handle a form close event in vb.net

I have used the below code but its not showing the msgbox. What is wrong with this code ?
Private Sub frmSimple_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
Dim result = MsgBox("Are you sure you want to Exit ?", vbYesNo)
If result = DialogResult.Yes Then
me.Close()
End If
End Sub
This code runs after the form has been closed, when it's being disposed.
Depending on how you're showing the form, it might not get disposed at all.
You need to handle the FormClosing event and set e.Cancel to True if you want to cancel the close.
Private Sub frmProgramma_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Are you sur to close this application?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Else
e.Cancel = True
End If
End Sub
or that is how i use it everytime over and over...
Use FormClosing event. MSDN
Dim result = MsgBox("Are you sure you want to Exit ?", vbYesNo)
If result = vbYes Then
me.Close()
End If
If MessageBox.Show("¿Exit?", "Application, MessageBoxButtons.YesNo, _
MessageBoxIcon.Question) = DialogResult.No Then
e.Cancel = True
End If
I think it is more clean and simply!
If MsgBox("Are you sure you want to Exit ?", vbYesNo) = vbNo Then e.Cancel = True
This code may not be 'efficient' but allows the user to save their work before closing, close the form if they press 'No' or return back to the form without closing if they press 'Cancel'.
Dim dialog As DialogResult
dialog = MessageBox.Show("Save before closing?", "Exit", MessageBoxButtons.YesNoCancel)
If dialog = DialogResult.Yes Then
'Put a save file dialog here or Button.PerformClick() if you already have a save button programmed
ElseIf dialog = DialogResult.No Then
Application.Exit()
ElseIf dialog = DialogResult.Cancel Then
e.Cancel = True
End If