RESOLVED - prevent user from closing via taskbar icon
Hello,
I have a VB2010 forms app running primarily on Win 7 boxes. In the app itself, programming is such that the user cannot exit the program without appropriate prompts (i.e. "are you sure", etc.) and form close buttons are disabled when appropriate, etc.
However, if the user hovers over the program's icon on the task bar, and then hovers the resulting image, that image has a "red X" which they can click on and the app will close, no questions asked. Is there a way to prevent this behavior? (Setting "Show in Taskbar" to false is not an option as the app must show in the taskbar. The only other option I could think of was to code for the FormClosing event and test the CloseReason for each and every one of the numerous forms in this application, which seems cumbersome.)
Your help is appreciated ...
Thanks,
Bruce
Re: prevent user from closing via taskbar icon
This should work
Code:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Are you sure?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
End Sub
You can also use e.CloseReason to know the reason of form closing and show the confirmation accordingly, e.g. you may prefer not show the message if e.CloseReason = CloseReason.WindowsShutDown
Re: prevent user from closing via taskbar icon
Hi 4x2y,
Thanks for your response. However, since this is a MDI app, I would have to add that kind of code to every form, which I was hoping to avoid. I tested it on one form and close reason shows as "UserClosing". In any case, if I have to do this for each form, so be it, but I was hoping there was a way to tell the system generally, "do not allow this app to be closed via the taskbar button".
Re: prevent user from closing via taskbar icon
You can create your form class with that code and use it instead of standard form
Code:
Option Strict On
Option Explicit On
Public Class MyForm
Inherits System.Windows.Forms.Form
Public ConfirmExit As Boolean = True
Protected Overrides Sub OnFormClosing(e As FormClosingEventArgs)
If ConfirmExit Then
If MessageBox.Show("Are you sure?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
End If
MyBase.OnFormClosing(e)
End Sub
End Class
Re: prevent user from closing via taskbar icon
Or if it shows in the taskbar only have 1 form that shows there and code the formclosing on that form only, all other forms are not to show in taskbar..
Re: prevent user from closing via taskbar icon
bensonsearch - thank you very much, your solution did the trick!
Re: prevent user from closing via taskbar icon
Quote:
Originally Posted by
BruceG
bensonsearch - thank you very much, your solution did the trick!
Happy to help :) saves coding x amount of forms lol