好的,所以我有一个名为 Form1.menuStrip1 的 MenuStrip。Form1.menuStrip1 有两个水平的项目,分别称为 Menu 和 Lists。我想要做的是在列表菜单中添加一个新的子菜单项。我有一个名为 mystring 的字符串,我希望它成为新项目的标题。
像这样的东西
void AddItem(string mystring)
{
// add mystring
}
这是示例
ToolStripMenuItem fileToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
ToolStripMenuItem loadLogsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
fileToolStripMenuItem1.Name = "File";
loadLogsToolStripMenuItem1.Name ="Logs";
this.fileToolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.loadLogsToolStripMenuItem1
}
如果您想向文件添加更多子项,那么您将拥有文件->日志,只需在 this.loadLogsToolStripMenuItem1 之后使用添加更多。
正确的代码始终在设计器代码中。您应该只在 VS 设计器中创建菜单和子菜单并检查设计器代码...观看此代码如何完成。然后根据需要删除。然后在动态运行时使用类似的代码。
像这样的东西应该做你需要的
void AddItem(string mystring)
{
ToolStripMenuItem item = new ToolStripMenuItem("Menu");
item.Text = myString;
MainMenu.Items.Add(item);
}