VB.NET overloads the default function when the user presses the X (Close Program) button

How do I override the default functionality when a user clicks on X in an application based on a VB.NET form?

I am currently handling the MyBase.Closing event ... but since this is a DirectX application, I need to do some cleanup before allowing the form to close.

+1
override event-handling events
Oct 13 '09 at 20:45
source share
3 answers

If you want to do additional cleanup before closing the form, do it on the form when the form closes .

This event also allows you to cancel the form that closes through the event property FormClosingEventArgs.Cancel . The CloseReason event property will tell you why the event was triggered (you should check the CloseReason.UserClosing )

+3
Oct 13 '09 at 20:50
source share

Repeat your other comments; doing anything before the event was fired would be premature; in the end, some other code may cancel the closure, and you will be left with a rebound. Personally, I will be tempted to override OnClosed ; not much of VB's head, but in C #:

  protected override void OnClosing(CancelEventArgs e) { // check OK to close (save/cancel etc) base.OnClosing(e); } protected override void OnClosed(EventArgs e) { // your code here base.OnClosed(e); // or here } 
+2
Oct 13 '09 at 20:59
source share

If you are not comfortable adding your code to the Form_Closing event, the only other option that I know of is a “hack”, which I used once or twice. No need to resort to this hacking, but here it is:




Do not use the normal close button. Instead, create your form so that it does not have a ControlBox. You can do this by setting ControlBox = false on the form, in which case you will still have a normal bar at the top of the form, or you can set the FormBorderStyle form to "None". If you follow this second route, there will not be a single bar at the top or any other visible border, so you will have to simulate them either by drawing a form or by using the Panel controls artfully.

Then you can add a standard button and make it look like a close button and put the cleanup code there. At the end of the button event, just call this.Close() (C #) or Me.Close() (VB)

+1
Oct 13 '09 at 20:57
source share



All Articles