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
Reply

Join Date: Feb 2008
Posts: 4
Reputation: igalep132 is an unknown quantity at this point 
Solved Threads: 0
igalep132 igalep132 is offline Offline
Newbie Poster

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

 
0
  #1
Sep 12th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,035
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 302
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

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.
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: igalep132 is an unknown quantity at this point 
Solved Threads: 0
igalep132 igalep132 is offline Offline
Newbie Poster

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

 
0
  #3
Sep 12th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,035
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 302
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

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

 
0
  #4
Sep 12th, 2009
You could also set the ControlBox property of the Form to false.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,410
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 614
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

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:
  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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,410
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 614
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

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!

  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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 348
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 44
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

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.
Reply With Quote Quick reply to this message  
Reply

Tags
eventcloseformc#

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for eventcloseformc#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC