[英]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.