Listview右键菜单的问题
本帖最后由 mpy2003 于 2013-11-04 13:00:02 编辑
首先代码生成人菜单
private void listView_MouseClick(object sender, MouseEventArgs e)
{
if (Program.Usergroup == "工程部" && e.Button == MouseButtons.Right && this .listView .SelectedItems .Count >0)
{
ContextMenuStrip cms = new ContextMenuStrip();
cms.Name = "cms";
cms.Items.Add("查看原始目录", null);
cms.Items.Add("修改非标信息", null);
this.ContextMenuStrip = cms;
cms.Show(listView,e.Location);
}
}
问题:
开始右键是没有菜单的,然后在Listview右键一项后出现菜单,然后不管是在Listview空白的地方还是窗体的任何一个地方右键都会出现这个菜单,好郁闷啊,要怎么样解决这个菜单只能在Listview里面选中行的时候右键才出现?
补充一个问题,可不可以图标从ImageList里面添加,代码怎么写?
------解决方案--------------------private ListViewItem GetItemFromPoint(ListView listView, Point mousePosition)
{
// translate the mouse position from screen coordinates to
// client coordinates within the given ListView
Point localPoint = listView.PointToClient(mousePosition);
return listView.GetItemAt(localPoint.X, localPoint.Y);
}
判断GetItemFromPoint!=null
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
------解决方案--------------------触发事件 前面先判断是否选中项 没选就return
------解决方案--------------------
if (listview.SelectedIndices != null && listview.SelectedIndices.Count>0)
------解决方案--------------------private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (!e.IsSelected)
{
&