Click here to Skip to main content
11,647,170 members (70,296 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Exception bugs WinForm
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 | :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 | :confused:

I would really appreciate a solution or workaround. Rose | [Rose]
This thing has had me tiring my hair out! Sigh | :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 5-May-10 5:06am
Edited 5-May-10 5:44am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

            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[^]
  Permalink  
Comments
Cnight Stalker at 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 at 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)

  Print Answers RSS
0 jyo.net 500
1 F-ES Sitecore 360
2 DamithSL 335
3 OriginalGriff 322
4 CPallini 290
0 OriginalGriff 1,277
1 jyo.net 994
2 DamithSL 946
3 Sergey Alexandrovich Kryukov 853
4 CPallini 760


Advertise | Privacy | Mobile
Web02 | 2.8.150804.2 | Last Updated 5 May 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100