0

I have a container form and a child window. The child window searches for the controls and updates the visibility status in database for that control. But after doing that I cannot close the child or parent window and it is throwing an exception unable to cast object of type 'System.Windows.Forms.ToolStripSeparator' to type 'System.Windows.Forms.ToolStripMenuItem'. I tried to catch the exception where the casting is done. But it is not showing the exception there. Please help

Below is the code for looping through menu.

for (int i = 0; i < toolStripItems.Count; i++)
{
    ToolStripMenuItem mi = toolStripItems[i] as ToolStripMenuItem;
    if (mi != null)
    {
        oldMenuToolTips.Add(mi.Name, mi.ToolTipText);
        mi.ToolTipText = mi.Name;

        if (mi.DropDownItems.Count > 0)
        {
            ShowToolStipItems(mi.DropDownItems);
        }

        PageControls.Items.Add(mi.Name);
    }
}

I am trying the code from this tutorial https://www.simple-talk.com/dotnet/windows-forms/controls-based-security-in-a-windows-forms-application/

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Akhilesh
  • 1,243
  • 4
  • 16
  • 49

1 Answers1

0

There was form closing event handler code in the child window. I had modified it from

foreach (ToolStripMenuItem mi in toolStripItems)
        {
            if (mi.DropDownItems.Count > 0)
            {
                RestoreMenuStripToolTips(mi.DropDownItems);
            }

            if (oldMenuToolTips.ContainsKey(mi.Name))
            {
                mi.ToolTipText = oldMenuToolTips[mi.Name];
            }
            else
            {
                mi.ToolTipText = string.Empty;
            }       
        } 

to

for (int i = 0; i < toolStripItems.Count; i++)
           {
             ToolStripMenuItem mi = toolStripItems[i] as ToolStripMenuItem;
              if (mi != null)
            {
                if (mi.DropDownItems.Count > 0)
                {
                    RestoreMenuStripToolTips(mi.DropDownItems);
                }

                if (oldMenuToolTips.ContainsKey(mi.Name))
                {
                    mi.ToolTipText = oldMenuToolTips[mi.Name];
                }
                else
                {
                    mi.ToolTipText = string.Empty;
                }       // end else
            }
            }

Problem was with the toolstripseparator.

Akhilesh
  • 1,243
  • 4
  • 16
  • 49