|
-
Jun 30th, 2015, 03:53 PM
#1
Thread Starter
PowerPoster
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
Last edited by BruceG; Jun 30th, 2015 at 11:51 PM.
Reason: resolved
"It's cold gin time again ..."
Check out my website here.
-
Jun 30th, 2015, 05:05 PM
#2
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
Last edited by 4x2y; Jun 30th, 2015 at 05:10 PM.
-
Jun 30th, 2015, 05:49 PM
#3
Thread Starter
PowerPoster
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".
"It's cold gin time again ..."
Check out my website here.
-
Jun 30th, 2015, 06:24 PM
#4
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
-
Jun 30th, 2015, 08:23 PM
#5
Fanatic Member
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..
My CodeBank Submissions
- Listbox with transparency and picture support - Click Here
- Check for a true internet connection - Click Here
- Open Cash drawer connected to receipt printer - Click Here
- Custom color and size border around form - Click Here
- Upload file to website without user logins, includes PHP - Click Here
- List All Removable USB Storage Devices - Click Here
- Custom On/Off Slide Control - Click Here
- Insert multiple rows of data into one database table using parameters - Click Here
- Trigger USB/Serial Cash Drawer - Click Here
-
Jun 30th, 2015, 11:49 PM
#6
Thread Starter
PowerPoster
Re: prevent user from closing via taskbar icon
bensonsearch - thank you very much, your solution did the trick!
"It's cold gin time again ..."
Check out my website here.
-
Jul 1st, 2015, 12:38 AM
#7
Fanatic Member
Re: prevent user from closing via taskbar icon
 Originally Posted by BruceG
bensonsearch - thank you very much, your solution did the trick!
Happy to help saves coding x amount of forms lol
My CodeBank Submissions
- Listbox with transparency and picture support - Click Here
- Check for a true internet connection - Click Here
- Open Cash drawer connected to receipt printer - Click Here
- Custom color and size border around form - Click Here
- Upload file to website without user logins, includes PHP - Click Here
- List All Removable USB Storage Devices - Click Here
- Custom On/Off Slide Control - Click Here
- Insert multiple rows of data into one database table using parameters - Click Here
- Trigger USB/Serial Cash Drawer - Click Here
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|