如何用鼠标右键单击列表视图项目上的上下文菜单?

How can I add context menu on listview item right click with the mouse?

我希望能够单击列表视图控件中的 select 项,而不是用鼠标右键单击并显示一些内容。

这是我创建的服装控件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Search_Text_In_Files
{
    public partial class ListViewCostumControl : UserControl
    {
        public static ListViewControl lvnf;

        public ListViewCostumControl()
        {
            InitializeComponent();

            lvnf = new ListViewControl();
            lvnf.Location = new Point(50, 50);
            lvnf.Size = new Size(50, 50);
            lvnf.View = View.Details;
            lvnf.Dock = DockStyle.Fill;
            lvnf.SuspendLayout();
            lvnf.LabelEdit = true;
            lvnf.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
            lvnf.Columns.Add("", 984, HorizontalAlignment.Left);
            //lvnf.Columns.Add("Subject", 200);
            //lvnf.Columns.Add("Date", 300);
            lvnf.Sorting = SortOrder.None;
            //lvnf.ColumnClick += lvnf_ColumnClick;
            //lvnf.Click += lvnf_Click;
            //lvnf.SelectedIndexChanged += lvnf_SelectedIndexChanged;
            this.Controls.Add(lvnf);
            lvnf.ResumeLayout(false);
        }

        public class ListViewControl : System.Windows.Forms.ListView
        {
            public ListViewControl()
            {
                //Activate double buffering
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);

                //Enable the OnNotifyMessage event so we get a chance to filter out 
                // Windows messages before they get to the form's WndProc
                this.SetStyle(ControlStyles.EnableNotifyMessage, true);
            }

            protected override void OnNotifyMessage(System.Windows.Forms.Message m)
            {
                //Filter out the WM_ERASEBKGND message
                if (m.Msg != 0x14)
                {
                    base.OnNotifyMessage(m);
                }
            }
        }

        private void ListViewNFTest_Load(object sender, EventArgs e)
        {

        }
    }
}

比在 Form1 中:

ListViewCostumControl.lvnf.MouseClick += Lvnf_MouseClick;

而在活动中:

private void Lvnf_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)
            {
                var focusedItem = ListViewCostumControl.lvnf.FocusedItem;
                if (focusedItem != null && focusedItem.Bounds.Contains(e.Location))
                {
                    contextMenuStrip1.Show(Cursor.Position);
                }
            }
        }

但是在列表视图中用鼠标右键单击 selected 项目时它没有显示任何内容。

首先您需要声明上下文菜单和至少一个项目。

private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;

接下来您需要创建对象并将它们分配给您的 listView。

this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.contextMenuStrip1.SuspendLayout();
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItem1});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.ResumeLayout(false);

lvnf.ContextMenuStrip = contextMenuStrip1;

只要看看设计师在添加上下文菜单时做了什么。 您不需要处理鼠标事件,因为它是由上下文菜单自动完成的。而是使用 menu/item 事件。

如果您需要上下文菜单仅出现在选中的列表视图 item/items 上,那么您可以处理打开事件并在未选中任何项目时隐藏菜单。

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
    if (lvnf.SelectedItems.Count == 0)
        e.Cancel = true;
}