Reputation: 73
i wrote below cod and it executes. but i should keep mouse on menu("select device") to see it's sub items("TV" , "Radio") .i can't see them by clicking on menu("select device") just by keeping mouse.as you see i used click event but it dose not work correctly.
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();
}
Upvotes: 0
Views: 3813
Reputation: 73452
Not sure what you're trying to achieve. If you need event for clicking sub items you need to use DropDownItemClicked event.
void item_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
var clickedItem = e.ClickedItem;
//Do whatever with clickedItem
}
Am entirely not sure what are you doing in toolStripClick
method. It makes no sense at all, you're removing the menus and adding it back. Why so?.
Upvotes: 0
Reputation: 23793
You are subscribing to the parent menu item clicked ("Select Device"). This is useless as you program doesnt take action in that case.
What you want is to subscribe to each sub item in that menu :
item2.Click += new EventHandler(tv_Click);
item3.Click += new EventHandler(radio_Click);
Upvotes: 1