views:

3443

answers:

3

I have a ListBox and I want to add a context menu to each item in the list. I've seen the "solution" to have the right click select an item and suppress the context menu if on white space, but this solution feels dirty.

Does anyone know a better way?

+3  A: 

There's no other way: the context menu isn't owned by the item in the listbox but by the listbox itself. It's similar to the treeview control which also owns the context menu instead of the treenode. So whenever an item in the listbox is selected, set the context menu of the listbox according to the selected item.

Frans Bouma
+5  A: 

Just to elaborate a little further to what Frans has said...Even though the ListBox owns the ContextMenuStrip, you can still customize the items in the menu strip at the time it's opening. Thus customizing it's contents based on the mouse position within the listbox.
The example below selects the item in the listbox based on a right mouse click and then customizes a context menu strip based on the item the user right-clicked on. This is a simple example but should get you going: Add a listbox to a form and add this code:

print("        #region Private Members
    private ContextMenuStrip listboxContextMenu;
    #endregion

    private void Form1_Load( object sender, EventArgs e )
    {
        //assign a contextmenustrip
        listboxContextMenu = new ContextMenuStrip();
        listboxContextMenu.Opening +=new CancelEventHandler(listboxContextMenu_Opening);
        listBox1.ContextMenuStrip = listboxContextMenu;

        //load a listbox
        for ( int i = 0; i < 100; i++ )
        {
            listBox1.Items.Add( "Item: " + i );
        }
    }

    private void listBox1_MouseDown( object sender, MouseEventArgs e )
    {
        if ( e.Button == MouseButtons.Right )
        {
            //select the item under the mouse pointer
            listBox1.SelectedIndex = listBox1.IndexFromPoint( e.Location );
            if ( listBox1.SelectedIndex != -1)
            {
                listboxContextMenu.Show();   
            }                
        }
    }

    private void listboxContextMenu_Opening( object sender, CancelEventArgs e )
    {
        //clear the menu and add custom items
        listboxContextMenu.Items.Clear();
        listboxContextMenu.Items.Add( string.Format( "Edit - {0}", listBox1.SelectedItem.ToString() ) );
    } ");

Hope that help. -Mike

MikeM
+1  A: 

If its just a question of enabling or disabling context menu items, it might be more efficient to only do it when the context menu is launched rather than every time the list box selection changes:

myListBox.ContextMenu.Popup += new EventHandler(myContextPopupHandler);


private void myContextPopupHandler(Object sender, System.EventArgs e)
{
    if (SelectedItem != null)
    {
        ContextMenu.MenuItems[1].Enabled = true;
        ContextMenu.MenuItems[2].Enabled = true;
    }
    else
    {
        ContextMenu.MenuItems[1].Enabled = false;
        ContextMenu.MenuItems[2].Enabled = false;
    }
}
Adam Pierce