 |
|
 |
I am trying to implement a handler for mybase.closing in VB.NET 2005. I am getting the blue underline error message saying "Event 'Closing' was not found". I know your article was done in 2002, and that the rtool is significantly different, but is there anything that I can do to resolve this issue???
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
T
|
|
|
|
 |
|
|
 |
|
 |
Like the subject says, overriding WndProc on your form changes the GetMethod.Name properties, rendering this code pretty much useless. Closing with Task Manager even generates an error on GetFrame(14).
Me thinks it might even be easier to just check and handle the messages in WndProc anyway.
Still a very nice piece of code, though! Well done!
HanreG
|
|
|
|
 |
|
 |
I have recently converted an application that does this from VC++/MFC to C#. The following is a conversion of the method I used in that application by overriding OnSysCommand and essentially handling the event when it is a button press on the Close button/menu item and before it is translated into a request to close the window. private const int WM_SYSCOMMAND = 0x0112; // From winuser.h private const int SC_CLOSE = 0xF060; protected override void WndProc(ref Message m) { if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE) { // Perform actions } else base.WndProc (ref m); } N.B. I have also posted this to GotDotNet Gerard Krupa BSc AMBCS
|
|
|
|
 |
|
 |
I don't really have time to write an article on this, but here is a more complete implementation. I have tested on Windows 2000 only, so it may not work on 98/ME. Also, this has not been tested with MDI children or parents. Most times, you need to know if you are closing because the user selects "Close" from the menu, or whether it is some sort of system thing. The previous implementation does not take into account shutting down the computer. In this instance, where I am cancelling and hiding if it is user input, this would abort the shutdown! You'd never be able to shutdown your computer normally as long as your .NET program is running. private enum ClosingSource
{
Unknown = -1, Code = 0, TaskManager = 1, SystemShutdown = 2, FormControlMenu = 3 }
private ClosingSource GetClosingSource(StackTrace stack)
{
StackFrame frame7 = stack.GetFrame(7);
if(frame7.GetMethod().Name == "SendMessage")
return ClosingSource.Code;
else if(frame7.GetMethod().Name == "DispatchMessageW")
return ClosingSource.TaskManager;
else if(frame7.GetMethod().Name == "CallWindowProc")
{
StackFrame frame14 = stack.GetFrame(14);
if(frame14.GetMethod().Name == "WndProc")
return ClosingSource.SystemShutdown;
else if(frame14.GetMethod().Name == "WmSysCommand")
return ClosingSource.FormControlMenu;
}
return ClosingSource.Unknown;
}
private void EventLog_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
StackTrace stack = new StackTrace(true);
ClosingSource source = GetClosingSource(stack);
if(source == ClosingSource.FormControlMenu)
{
e.Cancel = true;
this.Hide();
}
}I don't like the stack-based solution for "QueryUnload" --> "Closing" much. It depends on a lot of underlying code that I don't have any control over not changing over time. However, I have yet to find a better method, or even another method that works. One of the few instances where .NET functionality is not a superset of VB6.
|
|
|
|
 |
|
 |
This is fine if I have a WinForm app. If I have a User Control housed within IE or a Win Exe, how do I determine (from within my User Control) that they've closed the app hosting my User Control?
|
|
|
|
 |
|
 |
You can't. Because it's really IE getting closed and not your User Control. What would you need this functionality for though?
Norm Almond: I seen some GUI's in my life but WTF is this mess
Leppie: I made an app for my sister and she wouldnt use it till it was colorful enough
Norm:good point leppie, from that statement I can only deduce that this GUI must be aimed at children
Leppie:My sister is 25
-Norm on the MailMagic GUI
|
|
|
|
 |
|
 |
That they HAVE closed it, or HOW it was closed?
|
|
|
|
 |
|
 |
This code appears to have been "borrowed" from a post by "YeahIGotDotNet" on the GotDotNet[^] message boards. Unless you are the original poster of this code, either remove it or include the appropriate credits.
|
|
|
|
 |
|
 |
Maybe he is the original poster. It could happen.
|
|
|
|
 |
|
 |
Not in this case. The original poster has been notified.
|
|
|
|
 |
|
 |
It says right in the article:
"The code if from GotDotNet message boards"
|
|
|
|
 |
|
 |
It doesn't even mention the name of the person who was stupid kind enough to post the code to the messageboards in the first place.
|
|
|
|
 |
|
 |
AHEM!!!
I didnt mean to steal any thing!
I was just so desperate in finding a code like this, so I decided to post it here so that other people can use it...
and the reason that I didnt post the person's name was that because I couldn't go to GotDotNet.com (site upgrade I guess).
The original poster is YeahIGotDotNet.
|
|
|
|
 |
|
 |
The name and email address of the poster are included in the notification email. Since you appear to be the person who originaly asked the question, you would have a copy of that message. If not, you would have seen the name when you copied the code from the forum.
Whilst I can appreciate your desire to share this code with other people, it would have been better to contact YeahIGotDotNet and ask him to post the code as an article here. Only if he had ignored the request for a long time, or told you that he was too busy to post an article, should you have posted the code here with his name and a link to the original thread.
Posting his code verbatim as an article, without including the credits, asking for permission, or even notifying him, is totally unacceptable. This kind of behaviour will discourage anyone from providing sample code in forum posts to help answer questions.
|
|
|
|
 |