发布网友 发布时间:2024-10-10 23:06
共4个回答
热心网友 时间:2024-10-10 23:28
你需要把ToolStripMenuItem强制转换为ToolStripDropDownItem类型才能添加子项,其实你自己看看Form1.Designer.cs里设计器自动生成的代码就会明白了,方法如下
private void button1_Click(object sender, EventArgs e)
{
((ToolStripDropDownItem)(((ToolStripDropDownItem)contextMenuStrip1.Items["aToolStripMenuItem"]).DropDownItems["eToolStripMenuItem"])).DropDownItems.Add(new ToolStripMenuItem("h"));
//或者
ToolStripDropDownItem ts = ((ToolStripDropDownItem)contextMenuStrip1.Items["aToolStripMenuItem"]);
ts = ((ToolStripDropDownItem)ts.DropDownItems["eToolStripMenuItem"]);
ts.DropDownItems.Add(new ToolStripMenuItem("h"));
}
热心网友 时间:2024-10-10 23:28
看代码,最后放在InitializeComponent()中
添加一个菜单的代码:
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItem2;
private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItem3;
this.ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ToolStripMenuItem1});
this.ToolStripMenuItem.Name = "ToolStripMenuItem";
this.ToolStripMenuItem.Size = new System.Drawing.Size(23, 20);
this.ToolStripMenuItem.Text = "A";//第一级
this.ToolStripMenuItem1.Click += new System.EventHandler(this.ToolStripMenuItem1_Click)
this.工ToolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ToolStripMenuItem2});
this.ToolStripMenuItem1.Name = "ToolStripMenuItem1";
this.ToolStripMenuItem1.Size = new System.Drawing.Size(152, 22);
this.ToolStripMenuItem1.Text = "B";//第二级
this.ToolStripMenuItem2.Click += new System.EventHandler(this.ToolStripMenuItem2_Click)
this.ToolStripMenuItem2.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ToolStripMenuItem3});
this.ToolStripMenuItem2.Name = "ToolStripMenuItem2";
this.ToolStripMenuItem2.Size = new System.Drawing.Size(152, 22);
this.工ToolStripMenuItem2.Text = "C";//第三级
this.ToolStripMenuItem3.Click += new System.EventHandler(this.ToolStripMenuItem3_Click)
热心网友 时间:2024-10-10 23:27
郁闷啊..简单的问题总是这么多人来抢..
热心网友 时间:2024-10-10 23:26
解决了吗??