日期:2014-05-18 浏览次数:20807 次
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) { bool showMenu=false; //要判断是否点击项,控件里至少要有一个项 if (this.listView1.Items.Count > 0) { Point point = this.listView1.PointToClient(MousePosition); ListViewHitTestInfo hitTest = this.listView1.HitTest(point); if (point.Y >= this.listView1.TopItem.Bounds.Top && hitTest.Item != null) {//点击的不是列且不是空白区域 showMenu = true; } } e.Cancel = !showMenu; }
------解决方案--------------------
通过对listview包含的contextMenuStrip的opening事件处理
如果listview 没有选中任何行,取消菜单的显示
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
e.Cancel = true;
}
}