Adding and Removing ToolStripMenuItem from MenuStrip and DropDownItems in WinForms

I have MenuStripwith some static elements. To this, MenuStripI add items programmatically. Some of these elements have child elements ( DropDownItems).

At some point, I would like to delete all the added items in order to recreate the menu with different items. How to do it right?

An example of a situation:

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

I could do:

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

It all works, but I'm not sure if it is safe. Maybe I should delete and destroy all elements and sub-elements one after another recursively?

+3
source share
1 answer
  • Use only method Remove, enough
  • . , .
  • Items.Clear(), DropDownItems.Clear(), .
+3

Source: https://habr.com/ru/post/1771901/


All Articles