簡體   English   中英

如何用鼠標右鍵單擊列表視圖項目上的上下文菜單?

[英]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);
                }
            }
        }

但是當用鼠標右鍵單擊列表視圖中的選定項目時,它沒有顯示任何內容。

首先,您需要聲明上下文菜單和至少一個項目。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM