C#中用代码添加工具栏控件,如添加一个下拉菜单,以及菜单子项?

是用代码添加!
2024-12-24 16:51:56
推荐回答(2个)
回答1:

你可以先在设计器里手动添加,然后在Designer.cs里找到相应代码剪切过来就行,如下
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripDropDownButton toolStripButton1;
private System.Windows.Forms.ToolStripMenuItem aAAToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem aAAAToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem bBBToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem cCCToolStripMenuItem;
public Form1()
{
InitializeComponent();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripButton1 = new System.Windows.Forms.ToolStripDropDownButton();
this.aAAToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.bBBToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.cCCToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.aAAAToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.Controls.Add(toolStrip1);
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(617, 25);
this.toolStrip1.TabIndex = 3;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripButton1
//
this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.aAAToolStripMenuItem,
this.bBBToolStripMenuItem,
this.cCCToolStripMenuItem});
this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(29, 22);
this.toolStripButton1.Text = "toolStripButton1";
//
// aAAToolStripMenuItem
//
this.aAAToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.aAAAToolStripMenuItem});
this.aAAToolStripMenuItem.Name = "aAAToolStripMenuItem";
this.aAAToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.aAAToolStripMenuItem.Text = "AAA";
//
// bBBToolStripMenuItem
//
this.bBBToolStripMenuItem.Name = "bBBToolStripMenuItem";
this.bBBToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.bBBToolStripMenuItem.Text = "BBB";
//
// cCCToolStripMenuItem
//
this.cCCToolStripMenuItem.Name = "cCCToolStripMenuItem";
this.cCCToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.cCCToolStripMenuItem.Text = "CCC";
//
// aAAAToolStripMenuItem
//
this.aAAAToolStripMenuItem.Name = "aAAAToolStripMenuItem";
this.aAAAToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.aAAAToolStripMenuItem.Text = "AAAA";
}

回答2:

比如说一个下拉菜单是cmbSelect
为它添加Items的代码是:
cmbSelect.Items.Add("Item1");

cmbSelect.Items.Add("Item2");

cmbSelect.Items.Add("Item3");

cmbSelect.Items.Add("Item4");
这样就有这样的4个item在下拉菜单中出现了。

另外可以到vs2008中的property中去查找你想添加的内容,然后那里呈现的关键字一般来讲就是你要在代码中添加的内容的关键字
比如,Property中Text一栏就是comboBox中的文本,而它对应的代码就是
cmbSelect.Text = "Item1";