Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a context menu to a ListBoxItem?

People also ask

How can you attach context menu to a control?

Place a C1MainMenu on the form, right-click New Command, and then select Edit from its context menu. The Link to Command designer appears. In the Link to Command designer, change the following command properties. Select ContextMenu from the Create a new command listbox.

How do I use context menu strip?

You can associate a ContextMenuStrip with any control, and a right mouse click automatically displays the shortcut menu. You can show a ContextMenuStrip programmatically by using the Show method. ContextMenuStrip supports cancelable Opening and Closing events to handle dynamic population and multiple-click scenarios.


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:

#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.


This way the menu will pop up next to the mouse

private string _selectedMenuItem;
private readonly ContextMenuStrip collectionRoundMenuStrip;

public Form1()
{ 
    var toolStripMenuItem1 = new ToolStripMenuItem {Text = "Copy CR Name"};
    toolStripMenuItem1.Click += toolStripMenuItem1_Click;
    var toolStripMenuItem2 = new ToolStripMenuItem {Text = "Get information on CR"};
    toolStripMenuItem2.Click += toolStripMenuItem2_Click;
    collectionRoundMenuStrip = new ContextMenuStrip();
    collectionRoundMenuStrip.Items.AddRange(new ToolStripItem[] {toolStripMenuItem1, toolStripMenuItem2 });
    listBoxCollectionRounds.MouseDown += listBoxCollectionRounds_MouseDown;
}

private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
    var info = GetInfoByName(_selectedMenuItem);
   MessageBox.Show(info.Name + Environment.NewLine + info.Date);
}

private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
    Clipboard.SetText(_selectedMenuItem);
}

private void myListBox_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.Right) return;
    var index = myListBox.IndexFromPoint(e.Location);
    if (index != ListBox.NoMatches)
    {
        _selectedMenuItem = listBoxCollectionRounds.Items[index].ToString();
        collectionRoundMenuStrip.Show(Cursor.Position);
        collectionRoundMenuStrip.Visible = true;
    }
    else
    {
        collectionRoundMenuStrip.Visible = false;
    }
}

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.


And Here it is My Solution :

listBox_Usernames.ContextMenuStrip = contextMenuStripRemove;
listBox_Usernames.MouseUp += new MouseEventHandler(listBox_Usernames_MouseUp);

void listBox_Usernames_MouseUp(object sender, MouseEventArgs e)
{
    int index = listBox_Usernames.IndexFromPoint(e.Location);
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        if (index != ListBox.NoMatches)
        {
            if (listBox_Usernames.SelectedIndex == index)
            {
                listBox_Usernames.ContextMenuStrip.Visible = true;
            }
            else
            {
                listBox_Usernames.ContextMenuStrip.Visible = false;
            }
        }
        else
        {
            listBox_Usernames.ContextMenuStrip.Visible = false;
        }
    }
    else
    {
        listBox_Usernames.ContextMenuStrip.Visible = false;
    }
}