private void Form1_Load(object sender, EventArgs e)
{
MakeItems();
}
//its a sample. the items("tv" , "radio",...) will change every moment.
//in my real project i get this items dynamically by every clicking on toolstripmenu ("select Device").
// because of this i remove items and add them again.
void MakeItems()
{
var item = new System.Windows.Forms.ToolStripMenuItem()
{
Name = "Test0",
Text = "select device"
};
item.Click += new EventHandler(toolStripClick);
var item2 = new System.Windows.Forms.ToolStripMenuItem()
{
Name = "Test1",
Text = "TV"
};
var item3 = new System.Windows.Forms.ToolStripMenuItem()
{
Name = "Test2",
Text = "Radio"
};
item.DropDownItems.Add(item2);
item.DropDownItems.Add(item3);
menuStrip1.Items.Add(item);
}
void toolStripClick(object sender, EventArgs e)
{
ToolStripItem myitem = (ToolStripItem)sender;
menuStrip1.Items.Remove(myitem);
MakeItems();
}
答案 0 :(得分:0)
您正在订阅单击的父菜单项("选择设备")。由于程序在这种情况下没有采取行动,这是没用的。
您想要的是订阅该菜单中的每个子项目:
item2.Click += new EventHandler(tv_Click);
item3.Click += new EventHandler(radio_Click);
答案 1 :(得分:0)
不确定你想要实现的目标。如果您需要点击子项目的活动,则需要使用DropDownItemClicked活动。
void item_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
var clickedItem = e.ClickedItem;
//Do whatever with clickedItem
}
我完全不确定你在toolStripClick
方法中做了什么。完全没有意义,你要删除菜单并将其添加回来。为什么这样?。