473,321 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Detecting X button click of a window in winforms.

88 64KB
How can I detect the click event of the close (X) button at the top right corner of the control box of a form/window? Please note, I don't want to know about CloseReason, FormClosing, FormClosed or stuffs like these, unless they are inevitable. I exactly want to detect if the user clicked the X button of the form. Thanks.
Feb 25 '12 #1
5 18420
Falkeli
19
As far as I know, there is no other way to detect this. Simply make sure to have a flag to be used to detect any other control set to close the form, and then check for e.CloseReason == CloseReason.UserClosing in the FromClosing event to make sure that the window isn't being closed by the parent or the OS.
Mar 1 '12 #2
GaryTexmo
1,501 Expert 1GB
The only other thing I might suggest is dig through the WinAPI functions and see if you can figure out where exactly that close button is. Then you can globally check to see if the mouse button is clicked. If so, and if the form is in focus, is the cursor within the bounds of the close button.

This will likely be tricky. It would be much easier to rely on the form closing event (or what have you), so perhaps you could describe why you need to know about the X button being clicked and what you want to accomplish? Maybe we can then help you accomplish that.
Mar 1 '12 #3
PsychoCoder
465 Expert Mod 256MB
Here's a sample you can try and see if it does what you need. It's not 100% tested but it should at least get you going down the right path. I know of no other way than using the Closing event to check for something like that

Expand|Select|Wrap|Line Numbers
  1. public const int SC_CLOSE = 61536;
  2. public const int WM_SYSCOMMAND = 274;        
  3. public bool close = false;
  4.  
  5. protected override void WndProc(ref Message msg)        
  6. {        
  7.     if (msg.Msg == WM_SYSCOMMAND && msg.WParam.ToInt32() == SC_CLOSE)            
  8.     {            
  9.         this.close = true;            
  10.     }
  11.  
  12.     base.WndProc (ref msg);        
  13. }
  14.  
  15. private Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)        
  16. {        
  17.     if (this.close)        
  18.     {        
  19.         // X button clicked..            
  20.     }        
  21. }
Mar 1 '12 #4
GaryTexmo
1,501 Expert 1GB
Slick thought, Psycho! It still gets Alt-F4 and the icon-click-close in addition to the X, but it doesn't get a call to Form.Close :)
Mar 2 '12 #5
Falkeli
19
Using a KeyDown event and a KeyUp event you could check for the user pressing alt (you get a KeyDown when the user presses it, and a KeyUp when he/she stops); use this to exclude alt-F4. The condition you need in the KeyDown and KeyUp events is:
Expand|Select|Wrap|Line Numbers
  1. if (e.KeyCode == Keys.Alt)
You need to set the form's KeyPreview property to true.
Mar 4 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Scott Schade | last post by:
I have a form onto which I add a control during execution. The control is a control that I wrote. The control has a number of controls on it. I would like to click on a button on the control and...
1
by: kk | last post by:
Hello everyone, Thank you in advance for your time and help. Mine is an asp.net/C# application. I have an aspx page(parent window) which has a link that launches a new ie window with another...
11
by: CW | last post by:
I have message entry screen that's causing me a bit of an issue. At the moment, there are 2 buttons, one is used to send message to another user (btnSend) and another is used to send messages to...
3
by: hb | last post by:
Hi, I have a asp:button btnGo. When clicking this button, the code will parse a asp:table to find a specific ID under certain conditions. Once the ID is found, the code need to keep the current...
2
by: adolf garlic | last post by:
I'm having a problem with a button click event being fired erroneously. My login.aspx page has the usual username/password boxes. There is also a link to click when you have filled these, plus a...
3
by: Imran Aziz | last post by:
Hello All, I have a search text and button that post data and my button handler filters the repeater control. However when the button is clicked the first time. The page_load event is being called...
6
by: shil | last post by:
I have a server side button that needs to do some preperation and afterwards, if everything goes well, open a new browser with a specific url. I know how to execute javascript from a server side...
1
by: ranabhavin2 | last post by:
Hi, on button click I want to open new window each time. Currently when I m click on button second time then my new window is replaced with my original window. We can do it in hyperlink by ...
2
by: lintas | last post by:
Hi All, I am using a java script function for opening a pop up window in a link button click. And I am able to call the function at link button's second click only. I am not able to call the...
4
by: sreemathy2000 | last post by:
I have a vague problem.. Inmy windows application in c#, i'm opening a child window from from my main window thru the show method. I have few events on the child window. After my button click...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.