在选定列表视图记录上显示右键单击选项 - Winforms C#.NET

pea*_*ace 3 .net c# vb.net winforms

替代文字http://img413.imageshack.us/img413/9417/snapshotapp.jpg

ContextMenuStrip绑定到ListView控件.但是,右键单击选项(编辑)出现在我单击ListView区域的位置,这给了我特殊的错误,因为编辑的实现只能处理选定的行.我只希望它出现在选定的行(蓝色突出显示的行)上.我该怎么做?

Han*_*ant 9

将ContextMenuStrip属性重置为(none).实现MouseUp事件处理程序并使用ListView.HitTest()来找出它被点击的位置.例如:

    private void listView1_MouseUp(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Right) {
            var loc = listView1.HitTest(e.Location);
            if (loc.Item != null) contextMenuStrip1.Show(listView1, e.Location);
        }
    }
Run Code Online (Sandbox Code Playgroud)