c# - 有没有办法将 ContextMenuStrip 分配给 ListViewItem?

标签 c# winforms listview listviewitem contextmenustrip

我想将相同的 ContextMenuStrip 分配给表单上的所有 ListViewItems。这些 ListViewItems 是动态创建的。

不幸的是,ListViewItems 似乎没有可以分配给的 ContextMenuStrip 属性(当然,ListView 本身有)。

我是否必须将 ContextMenuStrip 分配给 ListView,然后根据 ListView 当前选择的项目,从那里继续?

最佳答案

private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        var hitTestInfo = listView1.HitTest(e.X, e.Y);
        if (hitTestInfo.Item != null)
        {
            var loc = e.Location;
            loc.Offset(listView1.Location);

            // Adjust context menu (or it's contents) based on hitTestInfo details
            this.contextMenuStrip2.Show(this, loc);
        }
    }
}

关于c# - 有没有办法将 ContextMenuStrip 分配给 ListViewItem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10126744/

相关文章:

c# - 将 DataGridViewAutoFilter 与 Equin BindingListView 结合使用

c# - 实时消息/更新的长轮询

winforms - 从后台线程更新 UI,当线程完成时,该线程在主 UI 中循环调用

android - 如何在 Android 中的 Button Click 事件上删除项目后刷新 Listview?

android - Android中如何制作传统蒙文ListView

c# - Alt 键导致表单重绘

c# - 如何在 32 个二元期权之间迭代?

c# - 在c# Winform( Entity Framework )中搜索记录

c# - 与使用 GraphicsPath 的 Rectangle.Inflate() 等效的是什么?

python - 如何更改/添加 Django 的 ListView 的响应 header ?