C# - Disabling the close button of a form

Set the ControlBox property to False to remove icon, system menu and buttons from the title bar. I'm pretty sure there is no way to actually disable that button, i.e. have it be there but "greyed out", in .NET. You need to either get rid of it, which is a bit of a baby and bath water situation, or determine whether it was clicked and act accordingly. You can set a variable when a legal closing method is used and set e.cancelled to True in the form's Closing event handler if that variable is not set. You can also intercept the Windows Message that is sent when that button is clicked using the by overriding the WndProc method. I can't remember what the message is but I've seen mention of it several times in several different places.
 
jmcilhinney said:
I'm pretty sure there is no way to actually disable that button, i.e. have it be there but "greyed out", in .NET.

.NET no, using some old win32 methods it should be possible. I've done it in a VB5 application although that was such a long time ago that I dont remember the exact details ;).

Bit of googling and msdning:
http://support.microsoft.com/?kbid=245746
this is for an access database, but they dont use access methods (access doesnt support disabling the close button either) but they use win32 functions. This means you should be able to use this solution in .net as well.
 
Back
Top