Results 1 to 7 of 7

Thread: RESOLVED - prevent user from closing via taskbar icon

  1. #1

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

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

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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.



  3. #3

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    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.

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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



  5. #5
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    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

  6. #6

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    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.

  7. #7
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: prevent user from closing via taskbar icon

    Quote Originally Posted by BruceG View Post
    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
  •  



Click Here to Expand Forum to Full Width