I have a menu with a 'Windows' list that contains all mdi children thanks to the MdiList property. However there are other windows that are not inside the MDI container which I want to list in the menu. I can easily add them to the menu, but a problem arises when one of the MDI children has a menu. The menus are merged, but the menu before I have added the other windows to the list is merged with, and so is incorrect.
Using DockPanelSuite:
void menuWindow_Popup(object sender, EventArgs e)
{
// Because not all windows are MDI children anymore
// we need to find all the other windows and add them to the menu
// Delete whaterver pre-existed
while (menuWindow.MenuItems.Count > 1)
{
menuWindow.MenuItems.RemoveAt(1);
}
if (dockPanel.FloatWindows.Count > 0)
{
menuWindow.MenuItems.Add(new MenuItem("-"));
// Then add all the floating/docked windows
// Note MDIList takes care of the windows that are in MDI mode
foreach (Form dockContent in dockPanel.FloatWindows)
{
// each event handler closure needs its own form reference (not a shared one)
Form content = dockContent;
var mi = new MenuItem(content.Text,
(EventHandler)delegate(object s, EventArgs ea)
{
if (content.WindowState == FormWindowState.Minimized)
{
content.WindowState = FormWindowState.Normal;
}
content.Show();
content.Focus();
});
mi.Checked = (content == dockPanel.ActiveContent);
menuWindow.MenuItems.Add(mi);
}
}
}