Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When i adding MenuItem dynamically on a button click its working but error showing after creating MenuItem i try to get the name "mnuAAA" but i am getting null value.

C#
MenuItem newMenuItem1 = new MenuItem();
MenuItem newMenuItem2 = new MenuItem();
MenuItem newExistMenuItem = (MenuItem)this.mnuTest.Items[0];
newMenuItem1.Header = "AAA";
newMenuItem1.Name = "mnuAAA";
newExistMenuItem.Items.Add(newMenuItem1);
MenuItem newExistMenuItem2 = (MenuItem)this.mnuTest2.Items[1];
newMenuItem2.Header = "BBBBBB";
newMenuItem2.Name = "mnuBBBBB";
newExistMenuItem2.Items.Add(newMenuItem2);





MenuItem mnuMenu = (MenuItem)this.FindName("mnuAAA");
MessageBox.Show(mnuMenu.Name.ToString());


Thanks in adavance
Posted
Updated 12-Mar-15 3:36am
v8
Comments
Gold$Coin 13-Mar-15 2:33am    
have a "?" to which item you are trying to add the menu item? and, "this" stands for!! button? on what event you are trying to get the menu items back again? please paste the clear code.

1 solution

This should work - just verified. The Problem may be that you check before the menu is re-rendered. So create a second "Test-Button", add your menu dynamically, call Findname from another event-handler/command and it should work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900