首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将上下文菜单添加到ListBoxItem?

如何将上下文菜单添加到ListBoxItem?
EN

Stack Overflow用户
提问于 2008-12-18 04:32:14
回答 8查看 53.4K关注 0票数 30

我有一个ListBox,我想为列表中的每个项目添加一个上下文菜单。我见过这样的“解决方案”,即在空白区域右击选择一个项目并取消上下文菜单,但这个解决方案感觉很脏。

有没有人知道更好的方法?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2015-06-16 15:19:40

这样,菜单就会在鼠标旁边弹出

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;
    }
}
票数 5
EN

Stack Overflow用户

发布于 2008-12-18 14:45:25

尽管ListBox拥有said...Even,但为了进一步说明Frans拥有ContextMenuStrip的功能,您仍然可以在打开菜单条时自定义菜单条中的项目。从而根据鼠标在列表框中的位置自定义它的内容。

下面的示例基于鼠标右键单击来选择列表框中的项目,然后根据用户右键单击的项目自定义上下文菜单条。这是一个简单的示例,但应该可以让您上手:向表单添加一个列表框,并添加以下代码:

#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() ) );
}

希望能有所帮助。

票数 29
EN

Stack Overflow用户

发布于 2008-12-18 08:14:22

没有其他方法:上下文菜单不归列表框中的项所有,而归列表框本身所有。它类似于treeview控件,它也拥有上下文菜单,而不是treenode。因此,只要选择了列表框中的某一项,就根据所选的项设置列表框的上下文菜单。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/376910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档