2014-09-02 68 views
0

所以我有一個基本的佈局,我在這裏填充contextMenu的子菜單項與預設項目。我試圖找到所選子菜單項的'索引'。這有什麼方法?我找到了一種方法來查找主菜單項的索引,但找不到子菜單項。c#查找contextSubMenu項目索引點擊

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 contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) 
     { 
      //works only for main menu items 
      int index = contextMenuStrip1.Items.IndexOf(e.ClickedItem) 

      //need index of submenu ITEM CLICKED?? 
     } 

     private void contextMenuStrip1_Opening(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); 
      } 
     } 
    } 
} 
+0

你需要索引本身?或者你只是用它來查找產品? – 2014-09-02 18:18:04

+0

用它從列表中查找產品。因此,contextMenu預設列表中的第三項將是變量存儲列表中的第三項。 – JokerMartini 2014-09-02 18:22:34

+0

@DJKRAZE我在發佈這篇文章之前查看了你正在引用的帖子,並且我看到他正在使用'SubmenuItem_Click',在試圖研究他的結果時我找不到任何信息。 – JokerMartini 2014-09-02 18:28:32

回答

2

您可以執行以下操作。

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) 
    { 
     (contextMenuStrip1.Items[0] as ToolStripMenuItem).DropDownItems.Clear(); 
     (contextMenuStrip1.Items[0] as ToolStripMenuItem).DropDownItemClicked -= DropDownItemClicked; 
     (contextMenuStrip1.Items[0] as ToolStripMenuItem).DropDownItemClicked += DropDownItemClicked; 


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

    private void DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) 
    { 
     var parent = (ToolStripMenuItem)sender; 

     int index = parent.DropDownItems.IndexOf(e.ClickedItem); 
     Debug.WriteLine(index); 
    } 

但即使是更好地做到這一點:

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

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


    private void SelectedPreset(object sender, EventArgs e) 
    { 
     var menuItem = (ToolStripItem)sender; 
     Debug.WriteLine(((Product)menuItem.Tag).Name); 
    } 
+0

贏家!真棒。謝謝 – JokerMartini 2014-09-02 18:42:28

0

地址:

public class Product 
{ 
    // : 
    public override string ToString() { return Name; } 
} 

然後在contextMenuStrip1_Opening存儲整個對象,而不僅僅是名字。

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

     foreach (var p in productList) 
     { 
      ddi.Add(p, null, SelectedPreset); 
     } 
    } 
+0

有沒有submenuItem點擊事件,可以趕上發件人或e.click索引? – JokerMartini 2014-09-02 18:15:11