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

是用代码添加!... 是用代码添加! 展开
 我来答
dkxpsg
2009-02-25 · TA获得超过1628个赞
知道小有建树答主
回答量:592
采纳率:0%
帮助的人:1049万
展开全部
你可以先在设计器里手动添加,然后在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";
}
百度网友2bd870619
2009-02-25 · TA获得超过205个赞
知道答主
回答量:78
采纳率:0%
帮助的人:57.1万
展开全部
比如说一个下拉菜单是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";
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式