943,548 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 886
  • C# RSS
Sep 12th, 2009
0

button with an X at the upper-right corner of the form, how to catch this event @ C#

Expand Post »
hello,

which event is fired when i close form with X button ? i want event that fired only with X button pressed, i know that there is a FormClosing event, but the problem is that it fires each time when form is closed... also with frm.close()

and i don't want that happen

tanx
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
igalep132 is offline Offline
5 posts
since Feb 2008
Sep 12th, 2009
1

Re: button with an X at the upper-right corner of the form, how to catch this event @

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.
Last edited by ddanbe; Sep 12th, 2009 at 6:20 pm.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Sep 12th, 2009
0

Re: button with an X at the upper-right corner of the form, how to catch this event @

no,
what i meant to is that i want to catch some event which is fired when i press X button on the form and only then,
and if i have line like " frm.close()" in my code, the event wont be fired...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
igalep132 is offline Offline
5 posts
since Feb 2008
Sep 12th, 2009
0

Re: button with an X at the upper-right corner of the form, how to catch this event @

You could also set the ControlBox property of the Form to false.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Sep 12th, 2009
0

Re: button with an X at the upper-right corner of the form, how to catch this event @

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:
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb
  11. {
  12. public partial class frmImage : Form
  13. {
  14. private bool allowFormClose = false;
  15.  
  16. public frmImage()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. protected override void WndProc(ref Message m)
  22. {
  23. if (!allowFormClose && (m.Msg == 0x0010)) //form "X"
  24. {
  25. System.Diagnostics.Debugger.Break();
  26. //Do something
  27. }
  28. else
  29. base.WndProc(ref m);
  30. }
  31.  
  32. private void button2_Click(object sender, EventArgs e)
  33. {
  34. allowFormClose = true;
  35. this.Close();
  36. }
  37. }
  38. }

[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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 12th, 2009
0

Re: button with an X at the upper-right corner of the form, how to catch this event @

Wait -- You can do it!

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb
  11. {
  12. public partial class frmImage : Form
  13. {
  14. private bool allowFormClose = false;
  15.  
  16. public frmImage()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. protected override void WndProc(ref Message m)
  22. {
  23. if (!allowFormClose && (m.Msg == 0x0010)) //form "X"
  24. {
  25. //System.Diagnostics.Debugger.Break();
  26. //Do something
  27. }
  28. else
  29. base.WndProc(ref m);
  30. }
  31.  
  32. new void Close()
  33. {
  34. allowFormClose = true;
  35. base.Close();
  36. }
  37.  
  38. private void button2_Click(object sender, EventArgs e)
  39. {
  40. this.Close(); //will work
  41. }
  42.  
  43. private void button3_Click(object sender, EventArgs e)
  44. {
  45. (this as Form).Close(); //will not work
  46. }
  47. }
  48. }

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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 12th, 2009
2

Re: button with an X at the upper-right corner of the form, how to catch this event @

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.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: csv / search box problem
Next Thread in C# Forum Timeline: Adding parameters to crystal report programmatically.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC