tags:

views:

13

answers:

1

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);
            }
        }
    }
+1  A: 

Hmmm adding the following seemed to fix it:

        menuWindow = (MenuItem)sender;
        if (dockPanel.ActiveDocument != null)
        {
            foreach (MenuItem item in ((Form)dockPanel.ActiveDocument).MergedMenu.MenuItems)
            {
                if (item.Text == "&Window")
                {
                    menuWindow = item;
                }
            }
        }

But still leaving question open if there are better ways

Timothy Pratley