Override the "X" button

michael_hk

Centurion
Joined
Nov 24, 2003
Messages
199
Location
Hong Kong
Hi,

I have a strange winform that will not be closed even the user clicks on the "X" button at the top right hand corner (no response after clicking). I don't know the reason, but I guess this may be caused by the dynamic loading of controls to the form at runtime (I can't control this behaviour).

My Question is is it possible to handle the click event of the "X" button myself so that I can call Application.Exit()?

Thanks for your help.

Michael
 
Well basically when you click the close button, it's pretty much all linked to the form closing event. Perhaps you have code inside your form closing event, in particular e.cancel = true?
 
Thanks for your reply.

I can't find anything like e.cancel = true in the code and there is no closing event of the form. I guess this strange behaviour is the byproduct of something (I don't know what it is).
 
I'm afraid I can't. The solution contains more then 40 projects and I have no idea which project constitutes this behaviour. Therefore I am looking for a way to override the click event of the "X" button if such an event exists.

Thanks for your help though.
 
Winston said:
:| hmmm perhaps just posting up the actual forms code that contains that X button.

That single form contains > 1000 lines of code and I don't want to reveal any code in the project - the framework used in my company. Sorry about that. :o

AlexCode said:
Do you have any ActiveX control on your form?

No, all code is 'ordinary' and managed.
 
You have to overload the form's OnClosing event...

Code:
Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        e.Cancel() = True  ''cancels the closing event
        ''do other stuff here before closing...
        ''Me.Dispose if you want to still ditch it, but you
        ''probably don't since you're overriding this...
    End Sub
 
AlexCode said:
Do you have anything on the Validate or Validating Events of the form or any other child controls?

No.

Iceplug said:
You wouldn't happen to have any code running in a loop or something, would you?

No.

The main form itself does nothing (no business logic) but defines the layout of the application. All controls (labels, buttons, comboxes, etc) are loaded at run time and what controls got loaded depends on the configuration file (a XML file) and how the users interact with the form.

Thanks for you guys' replies.
 
Last edited:
That hppened to me once, and I was using the COM WebBrowser... AxWeb... don't remember the real reference name... and when the form were to be disposed it couldn't dispose the COM component correctly and the form wouldn't close...

Since you say that you're not using any COM components I really don't know what is going over there...

Sorry...

Alex :p
 
If you're going to do that, why not just override the X as I posted earlier and put the application.exit() call there and avoid cluttering your form with an exit button.

Plus putting a button on the form still doesn't stop the user from hitting the X to "close" it. If they do it will continue to run.

It might be worth a shot anyway, but I'd understand since you've been hitting this problem for a while now.
 
He can hide the ControlBox of the form too... so no X button would appear...
By no means it's the best way but it can be the way to gain some time...

Alex :p
 
Like mskeel said, you should be able to just override OnClosing. Although I'd prefer to override OnClosed, because at that point, you know the form is closing. But it might be possible that something else is handling the Closing event and cancelling it, which would prevent your form from closing. In that case, you might want to find the offending code or just call Application.Exit from OnClosing.

If you want to override OnClosed, it'd look like this:
Visual Basic:
    Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
        Application.Exit()
        'it's generally a good idea to call the base implementation when you override something
        MyBase.OnClosed(e)
    End Sub

..and if you want to override OnClosing instead, it's not that much different:
Visual Basic:
    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        Application.Exit()
        'it's generally a good idea to call the base implementation when you override something
        MyBase.OnClosing(e)
    End Sub
 
mskeel said:
If you're going to do that, why not just override the X as I posted earlier and put the application.exit() call there and avoid cluttering your form with an exit button.

Plus putting a button on the form still doesn't stop the user from hitting the X to "close" it. If they do it will continue to run.

It might be worth a shot anyway, but I'd understand since you've been hitting this problem for a while now.

Code:
// Neither works

protected override void OnClosed(System.EventArgs e) 
{
	MessageBox.Show("Closed"); // never get executed
	System.Windows.Forms.Application.Exit();
}

protected override void OnClosing(CancelEventArgs e) 
{
	MessageBox.Show("Closing");  // never get executed
	System.Windows.Forms.Application.Exit();
}

Thanks for your suggestion though it's not working.
 
Add a messagebox like shown bellow...
If my idea is correct it shoud show the first messagebox but not the second...
This will prove thet this is a disposing problem... or not... :D

Visual Basic:
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    MessageBox.Show("Should reach here!")
                    components.Dispose()
                    MessageBox.Show("This will not show!!")
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

Alex :p
 
Thanks for your suggestion though it's not working.

So the X isn't even sending the message? At least that's what I would think if you can't override it...we now know the closing event isn't being ignored somewhere. We also know that you aren't stuck in a loop or activeX control.

And you can't post any code...

Do you have any new theories since you can see and walk through the code?
 
Back
Top