Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ContextMenuStrip bug: ToolStripMenuItem fires on main form's ContextMenuStrip, while a modal dialog is still showing - resulting in "Form that is already visible cannot be displayed as a modal...

When a "modal dialog" is active, the main form still accepts and fires ToolStripItem events residing on a ContextMenuStrip.

This is a very frustrating and unacceptable behaviour.

When I thought my form's designer code had become corrupt, I was led to rebuild my application's main form from scratch, but the problem re-emerged! :mad:

I was surprised to find no discussing about this problem at any programming forum. Could I possible be the only person to come across this so-obvious bug? :confused:

I would really appreciate a solution or workaround. :rose:
This thing has had me tiring my hair out! :sigh:

This is regarding Visual Studio 2008 SP1 .NET 3.5.

To reproduce:

Start a project and add an About Box to the project.
Add a ContextMenuStrip to the main form.
Add a MenuItem (with "F1" for the ShortcutKeys property, and "About" for the Text property) to the ContextMenuStrip.
Add a ToolStrip to the main form.
Add a DropDownButton to the ToolStrip.
Update DropDown property of DropDownButton to be the ContextMenuStrip.

Apply the following code to the main form:-


namespace WindowsFormsApplication1
{
	public partial class MainFrm : Form
	{
		public MainFrm()
		{
			InitializeComponent();
		}

		AboutDlg aboutDlg = new AboutDlg();

		private void toolStripMenuItem1_Click(object sender, EventArgs e)
		{
			aboutDlg.ShowDialog(this);
		}

		private void toolStripMenuItem2_Click(object sender, EventArgs e)
		{
			Close();
		}
	}
}


Now, run the program and press F1 to show the dialog, and press F1 while the "modal dialog" is still active ...and boom - InvalidOperationException
Posted
Updated 5-May-10 5:44am
v2

1 solution

C#
try
{
    aboutDlg.ShowDialog(this);
}
catch
{

}

This will make the error not appear however this is not recommended as the whole purpose of try and catch is to identify rising Exceptions. MSDN[^]
 
Share this answer
 
Comments
Cnight Stalker 5-May-10 11:42am    
Thanks for your efforts, but the problem remains.

I have various functions located on ContextMenuStrip, including 'MainFrm.Close()'. When a user presses the ShortcutKey for closing the main form, that event must not fire, not while there is a modal dialog. This is the whole point of a modal dialog - to suspend main form input, until the dialog is closed.

Unfortunately, your solution does not result the problem.
4277480 5-May-10 11:46am    
My answer was based on your question which doesn't mention the shortcut key for closing the form?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900