Solved

UnloadMode equivalent of Form_QueryUnload in VB, in .NET

Posted on 2003-10-29
12
1,590 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

11 Experts available now in Live!

Get 1:1 Help Now