我有一个列表框,我想上下文菜单列表中添加到每个项目。我见过的“解决方案”有右键选择一个项目,如果在空白抑制上下文菜单,但这种方法感觉很脏。

有谁知道一个更好的办法?

有帮助吗?

解决方案

此方式,菜单将下一个弹出鼠标

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;
    }
}

其他提示

只是为了阐述远一点什么弗兰斯说......即使ListBox中拥有的ContextMenuStrip,你还可以自定义菜单条中的各项它的开放时间。因此,基于列表框内的鼠标位置对其进行自定义的内容。结果 下面的例子中,选择在基于鼠标右键点击列表框的项,然后定制基于用户右键点击了项的上下文菜单条。这是一个简单的例子,但应该让你去:一个列表框添加到窗体,并添加以下代码:

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

希望帮助。 -Mike

有没有其他办法:右键菜单不被ListBox中的项目,但在列表框本身所拥有。它类似于TreeView控件还拥有上下文菜单,而不是树节点。所以每当选择在列表框中的项,根据所选择的项目设置的列表框的上下文菜单。

和这是我的解决方案:

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;
    }
}

在XAML它显示了这样的:

<ListBox>
    <ListBox.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Add User..."/>
            <MenuItem Header="Edit..."/>
            <MenuItem Header="Disable"/>
        </ContextMenu>
    </ListBox.ContextMenu>
</ListBox>

如果它只是启用或禁用上下文菜单项的问题,它可能是更有效的只有做时,上下文菜单中启动,而不是每次都在列表框中选择的变化:

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;
    }
}

<强>这个人是最好...

using System.Windows.Forms;

ContextMenuStrip menu;

this.menu.Items.AddRange(new ToolStripItem[] { this.menuItem });
this.listBox.MouseUp += new MouseEventHandler(this.mouse_RightClick);

private void mouse_RightClick(object sender, MouseEventArgs e)
{
    int index = this.listBox.IndexFromPoint(e.Location);
    if (index != ListBox.NoMatches)
    {
        menu.Visible = true;
    }
    else
    {
        menu.Visible = false;
    }
}

在一个代码行(多个用于结合):

var datasource = new BindingList<string>( new List<string>( new string[] { "item1" } ) );
listbox.DataSource = datasource ;
listbox.ContextMenu = new ContextMenu(
    new MenuItem[] { 
        new MenuItem("Delete", 
            new EventHandler( (s,ev) => 
            datasource.Remove(listbox.SelectedItem.ToString())
        )
    ) 
});

private void buttonAdd_Click(object sender, EventArgs e)
{
    datasource.Add( textBox.Text );
}   
//Create and Initialize the contextMenuStrip component
contextMenuStrip_ListaAulas = new ContextMenuStrip();

//Adding an Item
contextMenuStrip_ListaAulas.Items.Add("Modificar");

//Binding the contextMenuStrip with the ListBox
listBox_Aulas.ContextMenuStrip = contextMenuStrip_ListaAulas;

//The solution below 
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
    //select the item under the mouse pointer
    listBox_Aulas.SelectedIndex = listBox_Aulas.IndexFromPoint(e.Location);

    //if the selected index is an item, binding the context MenuStrip with the listBox
    if (listBox_Aulas.SelectedIndex != -1)
    {
         listBox_Aulas.ContextMenuStrip = contextMenuStrip_ListaAulas;  
    }
    //else, untie the contextMenuStrip to the listBox 
    else
    {
         listBox_Aulas.ContextMenuStrip = null;
    }
}

我不喜欢这样,这样的伟大工程,为我禁食。

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {
        if (Listbox.SelectedItem == null)
            e.Cancel = true;
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top