Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

UnloadMode equivalent of Form_QueryUnload in VB, in .NET

Posted on 2003-10-29
12
Medium Priority
?
1,723 Views
Last Modified: 2007-12-19
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 ?
0
Comment
Question by:Bande
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
12 Comments
 
LVL 12

Expert Comment

by:roverm
ID: 9640146
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
0
 

Author Comment

by:Bande
ID: 9640389
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 ?
0
 
LVL 12

Expert Comment

by:roverm
ID: 9640478
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
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 9644749
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.
0
 

Author Comment

by:Bande
ID: 9647715
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 80 total points
ID: 9652094
Here where it gets dicey.  You need to perform a little more detective work to find out that information:

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

  Const SC_CLOSE As Integer = &HF060&
  Const WM_SYSCOMMAND As Integer = &H112

  ' Detect a command.
  If m.Msg = WM_SYSCOMMAND Then

     ' Detect a Close command.
     If m.WParam.ToInt32 = SC_CLOSE Then

        MsgBox("Closed")

     End If

  End If

  ' Pass on all messages to the default WndProc for the form.
  MyBase.WndProc(m)

End Sub
0
 
LVL 12

Expert Comment

by:roverm
ID: 9652126
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9652189
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.
0
 
LVL 12

Expert Comment

by:roverm
ID: 9652265
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?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10232198
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
0
 
LVL 12

Expert Comment

by:roverm
ID: 10233254
*lol* Not much points avail.... :))
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question