Adding and removing ToolStripMenuItem's from MenuStrip and DropDownItems in WinForms

久未见 提交于 2019-12-13 14:07:50

问题


I have MenuStrip with some static items. To this MenuStrip I am adding items programmatically. Some of these items has child items (DropDownItems).

At some point I would like to remove all added items to recreate menu with different items. How to do it right?

Example situation:

mainMenu
 -staticItem1
 -added1
  -added1_sub1
  -added1_sub2
 -added2
  -added2_sub1

I could do:

added1.Dispose();
mainMenu.Items.Remove(added2);

Both of this works, but I am not sure if it's safe. Maybe I should remove and dispose all items and subitems one by one recursively?


回答1:


  • Use Remove method only, it's enough
  • You don't need recursive, When you remove a parent all its children will be removed
  • Use Items.Clear() to remove all the children for the Menu and DropDownItems.Clear() to remove all the children for an item.


来源:https://stackoverflow.com/questions/4041815/adding-and-removing-toolstripmenuitems-from-menustrip-and-dropdownitems-in-winf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!