Hello All,
I have a bit of a problem here and I can't seem to find a solution. I need to know if there is a way to determine if a user has closed one of my application forms by clicking the 'X' or from the form's control menu. My App has code that needs to execute if this condition occurs and only if the condition occurs. Similar code is executed if the user closes the form by regular means, but I need to cover myself by having it execute in both cases. In VB 6 you could check for the condition by checking the UnloadMode of the form to see if it was equal to vbFormControlMenu. This does not seem to be an option in .Net. The closest option is UserClosing which covers all possibilities including the user clicking the close button. When I use this option it causes the code in the formClosing event to fire anyway dispite the fact that I have already executed the necessary code in the form's close button click event and thereby kicks me into a loop resulting in a stack overflow error. I would be grateful if somebody would be kind enough to shed some light on how to fix this problem. Below is the code as it is written in VB 6 and the .Net equivalent code.
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
clsApp.Windows.CloseForm Me
End If
e.Cancel =
TrueclsApp.ApplicationForms.Close(
Me) End If
Determining When The User Has Closed A Form By Clicking The 'X' or From The Control Menu
AT05
and the event is not executing
Is it a multiple form program
GMon
Thinking about it more, if you want the Close command on the Control Menu (system menu) to do something different, then remove it from the menu, and add in your own menu with a specific message as to what it does.
I've done something similar: however, I simply changed the displayed 'Close' text to 'Minimize to tray', because the form cannot be closed that way, and can only be 'closed' by a Menu item. Regardless, the X, Close and Alt+F4 all do the same thing (specifically, minimizes it to the system tray).
kgreer
So the short answer is no. The long answer is yes you can. But you say 'non standard': what is non-standard With all due respect, I think the point is that the X/Control Menu Close/Alt+F4 are the same and standard way of closing a form: VB6 gave the ability to provide 'non-standard' functionality.
This really comes down to accessability: specifically, ALT+F4 should do exactly the same thing as the X. If they don't, you are doing something different for a keyboard user verses a mouse user. It's crucial to understand that .NET brought VB inline with standard functionality - which specifically meant breaking away from VB6.
If you are determined to do it the way you want, then you will have some work (going against the flow is always work): modify the control menu using the Win32 API Calls AppendMenu, Modify Menu, etc. (they are in the User32 DLL, and information on them on www.pinvoke.net). If you like, you can even remove the Close button (and then get reports that the program won't close, and they have to kill the application with Task Manager). Remember, the objective of writing programs is so that the users get what they need, and not to educate them on what the programmer thinks should be done.
Edited to add: if we are talking about 'a close button' as opposed to 'the close button' (the X) then what's the problem Execute the code in the formclosing event. Really, I suppose, the Control Close/X/AltF4/Close Button all (should) do the same thing, right Don't do anything when a Close Button is pressed - do it all in the FormClosing event.
Hugo Vale
Thanks for all the replies to my posting, it is appreciated, but you all seem to be missing the point. In VB 6 I had the ability to check to see if a form was closed by using it's control menu or by clicking the X. It does not appear that this is possible cleanly in .Net without using things like boolean variables. Silly if you ask me. The point is, as long as the user clickes the close button on the form everything is cool, the necessary code will be executed, but I don't have that assurance, hence the code is also in the FormClosing event but should only be executed if the user chose a non standard way of closing the form. VB 6 gave me the means to control this cleanly, does .Net provide a similar mechanism or not
Hakan Gumus
Charlie Schreiner
This issue has come up a number of times in the newsgroups, most recently at http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_frm/thread/c1bb8c32c77d8ea1/ba0e849bc599d7fc q=Q%3Aclosing+a+form&rnum=1#ba0e849bc599d7fc. Do a google group search on "closing a form" and you should get plenty of hits.
Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
sweens319
I can think of two things to try... Try the form's closing event. Also if it is the form is the application itself, you can use application events to inform you of this
quadcell
jianshi
SebastienR
You can check on that easily enough in the debugger.
I'm wondering if there is a wndproc message from that button ...
cconrad
Um, need to clarify a few things: presumably, when you say Control Menu, you mean the top left Icon menu (It comes up, by default, with Close Alt+F4).
Note - this is important: The X, Control Menu Close and Alt+F4 should all do the same thing, regardless as to what it is: this will pass a CloseReason.UserClosing to the FormClosing event.
Likewise if you perform a me.close, you will get the same reason (in the FormClosing event). However, the difference is that you 'know' when the me.close command takes place: so, set a flag indicating that this is the case and read that flag in the FormClosing event.