所以我有一個基本的佈局,我在這裏填充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);
}
}
}
}
你需要索引本身?或者你只是用它來查找產品? – 2014-09-02 18:18:04
用它從列表中查找產品。因此,contextMenu預設列表中的第三項將是變量存儲列表中的第三項。 – JokerMartini 2014-09-02 18:22:34
@DJKRAZE我在發佈這篇文章之前查看了你正在引用的帖子,並且我看到他正在使用'SubmenuItem_Click',在試圖研究他的結果時我找不到任何信息。 – JokerMartini 2014-09-02 18:28:32