簡體   English   中英

使用上下文菜單項填充C#listview

[英]Populating C# listview with context menu item

我想知道如何正確地使用“右鍵單擊”上下文菜單中選定的預設項目中的信息填充表單中的標簽? 我目前正在使用每個類“產品”的“名稱”填充上下文菜單。 然后,我想填寫與用戶右鍵單擊菜單選擇的項目相對應的標簽。 上下文菜單項將隨着項目添加到列表而動態變化。

在此處輸入圖片說明

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

namespace rcMenu
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Product newProductA = new Product();
            newProductA.Name = "Ice Cream";
            newProductA.Category = "Dessert";
            newProductA.Price = "Free";
            productList.Add(newProductA);

            Product newProductB = new Product();
            newProductB.Name = "Cherries";
            newProductB.Category = "Produce";
            newProductB.Price = "$10.00";
            productList.Add(newProductB);

            Product newProductC = new Product();
            newProductC.Name = "Soda";
            newProductC.Category = "Beverage";
            newProductC.Price = "$1.99";
            productList.Add(newProductC);
        }

        public static List<Product> productList = new List<Product>();

        public class Product
        {
            public String Name { get; set; }
            public String Category { get; set; }
            public String Price { get; set; }
        }

        private void SelectedPreset(object sender, EventArgs e)
        {
            label1.Text = "Product Name: " + "SELECTED";
            label2.Text = "Product Category: " + "SELECTED";
            label3.Text = "Product Price: " + "SELECTED";
        }

        private void contextMenuStrip1_Opened(object sender, EventArgs e)
        {
            (contextMenuStrip1.Items[0] as ToolStripMenuItem).DropDownItems.Clear();

            foreach (var p in productList)
            {
                var itemName = p.Name;
                (contextMenuStrip1.Items[0] as ToolStripMenuItem).DropDownItems.Add(itemName, null, SelectedPreset);
            }
        }
    }
}

首先訂閱Opening事件,並放置如下代碼:

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if(contextMenuStrip1.Items.Count > 0)
            contextMenuStrip1.Items.Clear();

            foreach (var p in productList)
            {
                var itemName = p.Name;
                contextMenuStrip1.Items.Add(itemName);
            }
            e.Cancel = false;
        }

接下來訂閱ItemClicked事件,並放置如下代碼:

        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            Product p = productList.Find(i => i.Name == e.ClickedItem.Text);
            //just in case its null...
            if(p != null)
            {
               label1.Text = "Product Name: " + p.Name;
               label2.Text = "Product Category: " + p.Category;
               label3.Text = "Product Price: " + p.Price;
            }
        }

嘗試這個!

private void SelectedPreset(object sender, EventArgs e)
        {
            var p = productList.Where(x => x.Name == (sender as ToolStripMenuItem).Text).Single();

            label2.Text = "Product Category: " + (sender as ToolStripMenuItem).Text;
            label3.Text = "Product Price: " + p.Price;
        }

您必須擦亮一點,添加適當的驗證

暫無
暫無

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

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