【问题标题】:How can I add context menu on listview item right click with the mouse?如何在列表视图项上用鼠标右键单击添加上下文菜单?
【发布时间】:2021-08-21 07:59:55
【问题描述】:

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

这是我创建的服装控件:

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);
                }
            }
        }

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

【问题讨论】:

标签: c# winforms


【解决方案1】:

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

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;

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

如果您需要上下文菜单仅出现在 listView 选中的项目/项目上,那么您可以处理打开事件并在未选择任何项目时抑制菜单。

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

【讨论】:

    猜你喜欢
    • 2011-08-27
    • 2010-12-16
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 2012-12-09
    • 2010-10-16
    • 2021-08-19
    相关资源
    最近更新 更多