Solved

UnloadMode equivalent of Form_QueryUnload in VB, in .NET

Posted on 2003-10-29
12
1,646 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
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

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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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