2014-08-30 49 views
0

我想知道如何正確地填充我的表單中的標籤與來自在'右鍵單擊'上下文菜單中找到的選定預設項目的信息?我目前正在使用每個「產品」類的「名稱」填充上下文菜單。然後,我想填寫與用戶右鍵單擊菜單所選項目對應的標籤。上下文菜單項將隨着項目添加到列表而動態變化。填充C#listview與上下文菜單項

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

回答

1

首先訂閱開幕活動,並把這樣的代碼:

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

你能否返回itemClicked'index'列表中的項目號。例如,如果第二個項目在列表中點擊,那麼它會返回'2' – JokerMartini 2014-08-31 20:49:12

+1

@JokerMartini我認爲這樣做會這樣:int index = contextMenuStrip1.Items.IndexOf(e.ClickedItem); – terrybozzio 2014-08-31 21:15:13

+0

似乎不適用於子菜單項。這隻適用於主菜單項。 – JokerMartini 2014-09-02 00:59:17

0

試試這個!

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

你一定要擦亮了一下添加適當的驗證