【问题标题】:Listview Large Icon right click to open ContextMenuStripListview大图标右键打开ContextMenuStrip
【发布时间】:2015-10-06 00:22:41
【问题描述】:

在我的项目中,我有一个ListView,当我单击大图标中的右键时,我想打开我的ContextMenuStrip。我尝试了很多东西,但我没有成功。当我在ListView 内部右键单击时,ContextMenuStrip 会打开,但我想在我右键单击大图标时看到。

我还需要帮助获取点击图标的名称(属性)。

【问题讨论】:

  • 你试过我的代码了吗?我已经更新它以使用(垂直滚动的 LV)
  • 我试过了,但我不确定我是否理解正确:/我无法在我的列表视图项中使用您的代码:/
  • 你有什么问题?你之前有什么代码?
  • 我想在单击列表视图项目时打开 ContextMenuStrip,并且我的项目是动态创建的。
  • 我想学习如何使用这个动态项目属性

标签: c# winforms listview right-click contextmenustrip


【解决方案1】:

这是一个快速而肮脏的解决方案;请比我做更多的工作..

// a class level reference, prepare it where you want..
ContextMenuStrip ms = new ContextMenuStrip();

您应该编码MouseDownMouseUp 事件:

private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    // disassociate from listview at first:
    listView1.ContextMenuStrip = null;

    // check for right button
    if (e.Button != System.Windows.Forms.MouseButtons.Right) return;

    // get item info:
    ListViewHitTestInfo hi = listView1.HitTest(e.Location);

    // no item hit:
    if (hi.Item == null) return;

    // calculate the image rectangle:

    // this contains the unscrolled y coordinate:
    Point iloc = listView1.GetItemRect(hi.Item.Index).Location;
    // we combine it with the x-position:
    Rectangle r = new Rectangle(new Point (hi.Item.Position.X,  iloc.Y),
                                imageList1.ImageSize);
    // no image hit:
    if ( !r.Contains(e.Location) ) return;

    // maybe prepare or change the menue now..
    // here I display the image name from the keys array:
    ms.Items[0].Text = imageList1.Images.Keys[hi.Item.ImageIndex];
    ms.Location = e.Location;

    // associate with listview and show
    listView1.ContextMenuStrip = ms;
    ms.Show();
}

【讨论】:

    【解决方案2】:

    请您尝试以下方法,看看它是否有效... private void listView1_MouseClick(对象发送者,MouseEventArgs e) {
    if (e.Button == MouseButtons.Right) { if (listView1.FocusedItem.Bounds.Contains(e.Location) == true) { contextMenuStrip1.Show(Cursor.Position); } }
    }

    【讨论】:

    • 我尝试了这些代码,但是当单击列表视图中的某个位置时,contentMenuStrip 仍然有效。我只想在单击项目时查看 contextMenuStrip,并且我想获取项目属性以供使用。
    【解决方案3】:

    这应该可以工作

    private void listView1_MouseClick(object sender, MouseEventArgs e) { ListView listView = sender as ListView; if (e.Button == System.Windows.Forms.MouseButtons.Right) { ListViewItem item = listView.GetItemAt(e.X, e.Y); if (item != null) { item.Selected = true; contextMenuStrip1.Show(listView , e.Location); } } }

    在鼠标点击位置搜索列表视图项。如果它在那里,显示菜单............

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      相关资源
      最近更新 更多