繁体   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