【问题标题】:Is there a way to assign a ContextMenuStrip to a ListViewItem?有没有办法将 ContextMenuStrip 分配给 ListViewItem?
【发布时间】:2012-04-24 23:40:02
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# winforms listview listviewitem contextmenustrip


    【解决方案1】:
    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);
            }
        }
    }
    

    【讨论】:

    • 感谢您分享这个!
    猜你喜欢
    • 2023-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多