在WinForms中的MenuStrip和DropDownItems中添加和删除ToolStripMenuItem [英] Adding and removing ToolStripMenuItem's from MenuStrip and DropDownItems in WinForms

查看:501
本文介绍了在WinForms中的MenuStrip和DropDownItems中添加和删除ToolStripMenuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些静态项目的MenuStrip.我要以编程方式添加此MenuStrip的项目.其中一些项目具有子项目(DropDownItems).

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?

示例情况:

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

我可以做到:

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?

推荐答案

  • 仅使用Remove方法就足够了
  • 您不需要递归,删除父级时,所有子级都将被删除
  • 使用Items.Clear()删除菜单的所有子项,然后 DropDownItems.Clear()删除某项的所有子项.
    • 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.
    • 这篇关于在WinForms中的MenuStrip和DropDownItems中添加和删除ToolStripMenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆