所以我在这里有一个基本布局,我用一个预设项填充contextMenu的子菜单项。我正在尝试找到所选子菜单的“索引”。这有什么方法?我找到了一种方法来查找主菜单项的索引,但不是子菜单项。
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 :(得分: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);
}
答案 1 :(得分: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);
}
}