My application is in VB.net with 3.5 framework. I want to disable the close button ("[X]") on the main form of the application while doing some processing.
I have achieved the disabling by overriding the "ReadOnly Property CreateParams()" of the form. This is working fine, as both the control button on the form and close option on right click in the taskbar shows them as disabled.
This fulfills my needs on Windows XP but not on Windows 7. In Windows 7, right clicking on the application icon in the TaskBar shows a different menu...which has a new "Close window" option.
Close in the original menu still shows it as disabled (this old menu is hidden, but can be shown by holding Shift key and right click on the application icon in TaskBar). Now I need to disable this "Close window" option as well...and only for my application.
Is there a method for doing this programmatically?
Use:
Private Sub MyForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
End Sub
Edit:
Yes, you should check the e.CloseReason property. The sender's type is another parameter you can key off of to decide whether to cancel the close request or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With