| | |
button with an X at the upper-right corner of the form, how to catch this event @ C#
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
button with an X at the upper-right corner of the form, how to catch this event @ C#
0
#1 Sep 12th, 2009
Re: button with an X at the upper-right corner of the form, how to catch this event @
1
#2 Sep 12th, 2009
You mean you don't want the user to be able to click on it?
A FormClosing event happens before a Form is closed, a FormClosed event happens after a Form is closed. You don't need to implement these if you don't want to.
A FormClosing event happens before a Form is closed, a FormClosed event happens after a Form is closed. You don't need to implement these if you don't want to.
Last edited by ddanbe; Sep 12th, 2009 at 6:20 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
Re: button with an X at the upper-right corner of the form, how to catch this event @
0
#3 Sep 12th, 2009
Re: button with an X at the upper-right corner of the form, how to catch this event @
0
#4 Sep 12th, 2009
Re: button with an X at the upper-right corner of the form, how to catch this event @
0
#5 Sep 12th, 2009
As far as I know the "X" just sends the form close message to your form and you cannot distinguish the caller. This will work though:
[edit]
This is the functionally identical to just canceling the form close in the "FormClosing" event that ddanbe pointed out, with a lot less of a headache.
[/edit]
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace daniweb { public partial class frmImage : Form { private bool allowFormClose = false; public frmImage() { InitializeComponent(); } protected override void WndProc(ref Message m) { if (!allowFormClose && (m.Msg == 0x0010)) //form "X" { System.Diagnostics.Debugger.Break(); //Do something } else base.WndProc(ref m); } private void button2_Click(object sender, EventArgs e) { allowFormClose = true; this.Close(); } } }
[edit]
This is the functionally identical to just canceling the form close in the "FormClosing" event that ddanbe pointed out, with a lot less of a headache.
[/edit]
Last edited by sknake; Sep 12th, 2009 at 7:44 pm.
Re: button with an X at the upper-right corner of the form, how to catch this event @
0
#6 Sep 12th, 2009
Wait -- You can do it!
But this method doesn't work very well with inheritance and will probably break things down the road. You're still better off listening to Danny.
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace daniweb { public partial class frmImage : Form { private bool allowFormClose = false; public frmImage() { InitializeComponent(); } protected override void WndProc(ref Message m) { if (!allowFormClose && (m.Msg == 0x0010)) //form "X" { //System.Diagnostics.Debugger.Break(); //Do something } else base.WndProc(ref m); } new void Close() { allowFormClose = true; base.Close(); } private void button2_Click(object sender, EventArgs e) { this.Close(); //will work } private void button3_Click(object sender, EventArgs e) { (this as Form).Close(); //will not work } } }
But this method doesn't work very well with inheritance and will probably break things down the road. You're still better off listening to Danny.
Re: button with an X at the upper-right corner of the form, how to catch this event @
2
#7 Sep 12th, 2009
That's interesting sknake, I never would have thought of catching the close method like that.
But as far as all these answers go, the Simple response is the X button does only call the form to close. and however you catch that close call, either by wndproc or by a simple form closing event, the trick is to set a flag, just a simple Boolean variable, if its true, cancel the closing and handle your business, if its false, do nothing and allow the form to close.
when you need to call this.Close(); first change your variable. else you can create a public method for your form called DoClose() or something and change the flag there and call close, its up to you.
But as far as all these answers go, the Simple response is the X button does only call the form to close. and however you catch that close call, either by wndproc or by a simple form closing event, the trick is to set a flag, just a simple Boolean variable, if its true, cancel the closing and handle your business, if its false, do nothing and allow the form to close.
when you need to call this.Close(); first change your variable. else you can create a public method for your form called DoClose() or something and change the flag there and call close, its up to you.
![]() |
Similar Threads
- Starting wxPython (GUI code) (Python)
- Disable MDI parent form button from MDI child form (VB.NET)
- how to call an event inside another event (VB.NET)
- How to Minimize window form on closing? (C#)
- Disable Close button on form (C++)
- Plz Help:In windows programming how to close dialog box? (C++)
- How to Resize Image with form resize event (Visual Basic 4 / 5 / 6)
- Disable Close(X in upper right corner) button on Form (C++)
Other Threads in the C# Forum
- Previous Thread: csv / search box problem
- Next Thread: Adding parameters to crystal report programmatically.
| Thread Tools | Search this Thread |
Tag cloud for eventcloseformc#







