Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Dear All,

I have win-form application.

I want to prevent a closing application from the taskbar. I have added a button to close the application, the user should close the application by using this button only.

What I have tried:

I tried disabling mouse right click, but I don't think it a good solution.
private void uc_MouseClick(object sender, MouseEventArgs e)
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
         //more logic here

can anyone please help me. How can I prevent the closing application from outside?

Updated 4-Feb-19 3:01am

First off, you can't prevent the app from closing down "except on a button" - if nothing else any app or process can be killed from the Task Manager, and in that case your app doesn't even get a "shut down" command.

When a "normal" exit is happening rather than a forced task kill, you do get a FormClosing event, which you can handle - and that includes the taskbar "close window" command.
If you add a class level bool which is set in the button click event, you can set the FormClosingEventArgs.Cancel to true if you want to prevent closing the app. Your users will probably hate you though, I would.
Share this answer
My solution:
go back to your requirements and aks why you want to do this?
Consider better/other solutions: A Kiosk mode, not showing the Explorer in the first place, etc. and ask if you really need this…

Rule of thumb: What a user sees and looks common but doesn't work as expected will feel "wrong" - so don't mess with standard-menus and commands…

That said I have quite experience for this kind of things from past Projects where we had to emulate a "Kiosk mode". But we had access to the OS itself (Windows Embedded, and Windows CE)

You could use hooks, disable the Windows Control-box, don't show your app in the taskbar, etc.
I even once had to go that far to implement a Keyboarddriver to intercept Alt+Ctrl+Del (it's Special and not hookable like normal)

Again: If the users can see the taskbar - locking the user is not the way to go!
for all other solutions don't show your app in the taskbar or hide it completly

P.S. your idea with mouseclick won't work for the taskbar - you have to make a global hook
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900