JokerMartini
JokerMartini

Reputation: 6147

Populating C# listview with context menu item

I was wondering how I properly can populate the labels in my form with the information from the selected preset item found in the 'right-click' context menu? I'm currently populating the context menu with the 'name' of each class 'product'. I'd like to then fill in the labels corresponding to the item selected by the users right-click menu. The context menu items will change dynamically as items get added to the list.

enter image description here

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

Upvotes: 0

Views: 1742

Answers (2)

terrybozzio
terrybozzio

Reputation: 4542

First subscribe to the Opening event and place the code like this:

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

Next subscribe to the ItemClicked event and place the code like this:

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

Upvotes: 1

asb
asb

Reputation: 833

Try this!

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

you must polish up a bit add the proper validation

Upvotes: 0

Related Questions