子项添加到MenuStrip的ToolStripMenuItem

子项添加到MenuStrip的ToolStripMenuItem

本文介绍了如何在C#中将子项添加到MenuStrip的ToolStripMenuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体中添加了menustrip1,并向该menustrip1静态添加了一个toolstripmenuitem(WindowstoolStripmenuItem).我已经动态创建了一个工具条菜单.我想将此动态工具tripmenuitem添加到在设计时静态创建的静态menustripitem(WindowstoolStripmenuItem)中.

I have added a menustrip1 into my windows form and I statically added one toolstripmenuitem (WindowstoolStripmenuItem) to that menustrip1.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.

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

如何将此子项目添加到静态菜单条的Windows项中.

How can I add this subitem to the static menustrip's Windows item.

推荐答案

您可以将ToolStripMenuItem添加到另一个ToolStripMenuItem.DropDownItems集合中.

You can add a ToolStripMenuItem to another ToolStripMenuItem.DropDownItems collection.

如果没有对ToolStripMenuItem的引用,则可以通过键(名称属性)或索引获得一个

If you don't have a reference to your ToolStripMenuItem, you can get one by key (Name Property) or Index

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

这是代码

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);

这篇关于如何在C#中将子项添加到MenuStrip的ToolStripMenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:44