views:

326

answers:

1

I'm trying to make a context menu for a control that is "linked" to a main menu item. There are two fixed menu items that are always there and an arbitrary number of additional menu items that might need to be on the menu.

I've tried solving the problem by keeping a class-level reference to the fixed menu items and a list of the dynamic menu items. I am handling both menus' Opening events by clearing the current list of items, then adding the appropriate items to the menu. This works fine for the main menu, but the context menu behaves oddly.

The major problem seems to be that by the time Opening is raised, the menu has already decided which items it is going to display. This form demonstrates:

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class DemoForm : Form
    {
        private List _items;

        public DemoForm()
        {
            var contextMenu = new ContextMenuStrip();
            contextMenu.Opening += contextMenu_Opening;

            _items = new List();
            _items.Add(new ToolStripMenuItem("item 1"));
            _items.Add(new ToolStripMenuItem("item 2"));


            this.ContextMenuStrip = contextMenu;
        }

        void contextMenu_Opening(object sender, CancelEventArgs e)
        {
            var menu = sender as ContextMenuStrip;

            if (menu != null)
            {
                foreach (var item in _items)
                {
                    menu.Items.Add(item);
                }
            }
        }
    }
}

When you right-click the form the first time, nothing is displayed. The second time, the menu is displayed as expected. Is there another event that is raised where I could update the items? Is it a bad practice to dynamically choose menu items?

(Note: This is for an example I started making for someone who wanted such functionality and I was curious about how difficult it is, so I can't provide details about why this might be done. This person wants to "link" a main menu item to the context menu, and since menu items can only be the child of a single menu this seemed a reasonable way to do so. Any alternative suggestions for an approach are welcome.)

+1  A: 

You could work out the items during the MouseDown event of the control. Check that it is the right mouse button too.

Vincent McNabb
Pun not intended.
Vincent McNabb
Thanks! Had a similar problem with dynamically-generated ContextMenuStrip from a NotifyIcon, this solved the erratic behaviour I had using the ContextMenuStrip Opening event handler.
RedGlyph