简体   繁体   English

如何在 C# 中向 MenuStrip 的 ToolStripMenuItem 添加子项

[英]How to Add Sub Items to a MenuStrip's ToolStripMenuItem in C#

I have added a menustrip1 into my windows form and I statically added one toolstripmenuitem (WindowstoolStripmenuItem) to that menustrip1 .我已将menustrip1添加到我的 Windows 窗体中,并且我静态地向该menustrip1添加了一个toolstripmenuitem (WindowstoolStripmenuItem)。 And I have created a toolstripmenuitem dynamically.我已经动态创建了一个工具条菜单项。 I want to add this dynamic toolstripmenuitem to the static menustripitem(WindowstoolStripmenuItem) which is created statically on design time.我想将此动态工具条菜单项添加到在设计时静态创建的静态菜单条项(WindowstoolStripmenuItem)中。

ToolStripMenuItem itm = new ToolStripMenuItem();
itm.Name = "fm1";
itm.Text = "Form1";

How can I add this subitem to the static menustrip's Windows item.如何将此子项添加到静态菜单条的 Windows 项中。

You can add a ToolStripMenuItem to another ToolStripMenuItem.DropDownItems collection.您可以将ToolStripMenuItem添加到另一个ToolStripMenuItem.DropDownItems集合。

If you don't have a reference to your ToolStripMenuItem, you can get one by key (Name Property) or Index如果您没有对 ToolStripMenuItem 的引用,则可以通过键(名称属性)或索引获取

var itm = menustrip1.Items["Text"];
var itm = menustrip1.Items[0];

Here is the code这是代码

var menustrip1 = new System.Windows.Forms.MenuStrip();
var item = new System.Windows.Forms.ToolStripMenuItem()
{
    Name = "Test",
    Text = "Test" 
};
var item2 = new System.Windows.Forms.ToolStripMenuItem()
{
    Name = "Test",
    Text = "Test"
};
item.DropDownItems.Add(item2);
menustrip1.Items.Add(item);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示真正的菜单条的工具菜单及其子菜单? - How to visible true menustrip's toolstripmenuitem and its sub menu? 如何在C#中启用/禁用MenuStrip项和项的下拉集合项 - How to Enable/Disable MenuStrip Items and Item's Drop Down Collection Items In C# 如何在Windows应用程序C#中将所有活动表单名称添加到菜单项 - How to add all active forms names to menustrip items in windows application C# Windows Forms | MenuStrip 子项 (ToolStripMenuItem) 更改 Cursor 将不起作用 - Windows Forms | MenuStrip Sub-Items (ToolStripMenuItem) changing Cursor won't work C#将图标添加到ToolStripMenuItem - C# Add Icon To ToolStripMenuItem C#为什么在ToolStripMenuItem中无法获得带有子菜单项的菜单项? - C# why can't I get menu items with sub menu items in ToolStripMenuItem? C#放置MenuStrip.Items(子菜单) - C# Positioning of MenuStrip.Items (SubMenues) C#如何使Menustrip中的一项成为默认选择? 如何显示从“选定的菜单”到文本框中的选定项目 - C# How to make one item from Menustrip be default selected? How to display the selected items from Menustrip Selected to a textbox 将内容添加到menustrip C#并指定其代码 - add things to menustrip c# and specify their code MenuStrip子项打开方向 - MenuStrip sub items opening direction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM