Search code examples
c#winformsmenumdimdichild

Extra separator after MdiWindowListItem when no child windows are open


I have an MDI application. I have a ToolStripMenuItem labeled "Window" that is set as the MdiWindowListItem of my main MenuStrip. The Window menu has a couple of items, such as "Cascade" and "Arrange Icons".

When I run my application, the Window menu ends neatly with my "Arrange Icons" item. As expected, when I open one or more MDI child forms, these are automatically appended after a ToolStripSeparator. When I close all of the child forms, they disappear as expected from the "Window" menu, however the ToolStripSeparator remains. I would have expected the menu to return to its original state, i.e. without the separator.

As it stands, the separator is automatically added but not automatically removed. I could try to programmatically eliminate the extra separator, of course, but I don't see anyone else complaining about this, which leads me to suspect that it might be happening because I did something wrong. Please advise.


Solution

  • Try adding your own ToolStripSeparator menu item after your "Arrange Icons" menu items and set the Visible property to false.

    When opening an MDI child form, make it visible:

      Form f = new Form();
      f.Text = "Test Form";
      f.MdiParent = this;
      f.FormClosed += ChildClosed;
      f.Show();
      ts.Visible = true;
    

    Then in the ChildClosed method:

    void ChildClosed(object sender, FormClosedEventArgs e) {
      this.BeginInvoke((Action)delegate 
                          { ts.Visible = (this.MdiChildren.Count() > 0); }
                      );
    }