Link to home
Start Free TrialLog in
Avatar of Bande
Bande

asked on

UnloadMode equivalent of Form_QueryUnload in VB, in .NET

What is the equivalent of UnloadMode argument in Form_QueryUnload in VB , in .NET?
In VB it allows to check if the form is being closed from within the program or from outside , i.e with the 'X' button , but how do i do the same in .NET ?
Avatar of roverm
roverm
Flag of Netherlands image

Do you mean the 'Closing' event?

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

End Sub

D'Mzz!
RoverM
Avatar of Bande
Bande

ASKER

In VB there is a
     Form_QueryUnload( Cancel as Integer, UnloadMode as Integer)
which is called on the closing of the Form.Here 'UnloadMode' is used to identify if the Form is being closed by the user,in which case 'UnloadMode' is equal to 'vbFormCode' and not otherwise.
Using this, the programmer can prevent the user from closing a Form and only allow the form to be closed from within the Application.

There exists a Fom_Closing in .NET for the above , but it does not have a 'UnloadMode' parameter in it, and so what i wanted to know is how do i now find out if the user is trying to close the application, or if it is coming from within the application ?
The e argument contains a Cancel property. Set it to true and the form will not close.
The way the form is closed is not shown by this event and nor is another event.

You could try doing it by subclassing but I doubt if that gives you more result:

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    Debug.WriteLine(m.ToString)
    MyBase.WndProc(m)
End Sub

After running see the output for all commands.

Good luck!
D'Mzz!
RoverM
Avatar of Bob Learned
VB.NET does not have an  equivalent for the UnloadMode argument.  It is one of those things that initially got under my skin, since I routinely used it in VB6, but I find that now that I take a step back, I can change my way of thinking to do it another way.  

What kind of operation did you use the UnloadMode argument for?  I find that the Form_Closing event works for most of what I need to do.
Avatar of Bande

ASKER

In VB, the 'UnloadMode' is used to check for the above case. If the form is being closed by the user, then it has the 'vbFormCode' property, else not.

What i wanted to do is ,when the window is closed, i wanted to find out if this event is fired because, the user closed it or because of a close event from within the program.

One more problem here is that the form that iam haveing is being called in a Sub_Main function ( in .NET), and so, the program terminates, whatever be the case, and it ends up calling the Form_Closing event. What i want to do is,if the user tries to close,i want to display a messge box and cancel the event, and if the program closes,then i should just proceed .

If you have some idea about it , can you please tell me ?
Thanks in Advance.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
TheLearnedOne: This is what I adviced as well but I couldn't find the source of the close command.
And if I'm not mistaking, that is what Bande is searching for.

AFAIK: There's no way to detect the source of the sender of the close command.
The WM_SYSCOMMAND with a wParam of SC_CLOSE means the user clicked on the "X" button on the form.  All other cases, the form is closed by the WM_CLOSE message.
Oh great! I've learned something as well then!
I tried it using a button on the form and using the 'X' but I didn't notice a difference.

Bande: Can you use TheLearnedOne's example?
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: TheLearnedOne {http:#9652094}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
*lol* Not much points avail.... :))