Solved

UnloadMode equivalent of Form_QueryUnload in VB, in .NET

Posted on 2003-10-29
12
1,599 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
  • 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 20 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A short film showing how OnPage and Connectwise integration works.

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now