E
Eric Renken
OK, I have a form with a menu and a tool bar. In the Menu I have a File
menu that contains a "Exit" item. When I click this Exit item. I have an
event wired to:
mnuFile.DropDown.ItemClicked
In that event I check the:
event handler ...
if ( e.ClickedItem.Tag != null )
ProcessMenu( e.ClickedItem.Tag.ToString() )
System.Diagnostics.Debug.WriteLine( "Done" );
I do this because I have 1 function for all my menus on this screen. In
this function I have a switch statement:
switch ( tag )
{
....
case "Exit":
this.Close();
break;
....
}
This function returns up to the event handler successfully processes my
WriteLine statement, and then procedures to crash with:
{"Cannot access a disposed object.\r\nObject name:
'ToolStripDropDownMenu'."}
Why am I getting this error. There is no code after this and my code in my
Dispose processes correctly also. In stepping through the code the Dispose
happens before my this.Close() returns anyway, so I know all that is
functioning correctly. My thought on what is happening here is that at this
point after the event handler is done the ToolStrip is trying to hide the
drop down control, but since my form is already disposed of the ToolStrip is
causing an error internally. I think this because I can view the drop down
menu while stepping though my code. My thought is that after I clicked that
item the drop down should have disappeared on its own. Well anyway this
feels like a bug to me in the ToolStrip control, and I think I found a work
around. In my event I added this code to the beginning:
((ToolStripDropDown)sender).Hide();
By doing this I know longer crash when I close my application.
Eric Renken
menu that contains a "Exit" item. When I click this Exit item. I have an
event wired to:
mnuFile.DropDown.ItemClicked
In that event I check the:
event handler ...
if ( e.ClickedItem.Tag != null )
ProcessMenu( e.ClickedItem.Tag.ToString() )
System.Diagnostics.Debug.WriteLine( "Done" );
I do this because I have 1 function for all my menus on this screen. In
this function I have a switch statement:
switch ( tag )
{
....
case "Exit":
this.Close();
break;
....
}
This function returns up to the event handler successfully processes my
WriteLine statement, and then procedures to crash with:
{"Cannot access a disposed object.\r\nObject name:
'ToolStripDropDownMenu'."}
Why am I getting this error. There is no code after this and my code in my
Dispose processes correctly also. In stepping through the code the Dispose
happens before my this.Close() returns anyway, so I know all that is
functioning correctly. My thought on what is happening here is that at this
point after the event handler is done the ToolStrip is trying to hide the
drop down control, but since my form is already disposed of the ToolStrip is
causing an error internally. I think this because I can view the drop down
menu while stepping though my code. My thought is that after I clicked that
item the drop down should have disappeared on its own. Well anyway this
feels like a bug to me in the ToolStrip control, and I think I found a work
around. In my event I added this code to the beginning:
((ToolStripDropDown)sender).Hide();
By doing this I know longer crash when I close my application.
Eric Renken