如何在窗体上动态添加一个button?

halabazi 2004-11-22 09:29:54
我想在单击一个button的时候就在窗体上添加一个button,咋实现呢,好像挺基本的一问题,俺咋弄不出来呢.
...全文
246 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Happy2046 2004-11-22
  • 打赏
  • 举报
回复
楼上都说得很清楚了,我也没有什么可说得了^_^
cq_lqj 2004-11-22
  • 打赏
  • 举报
回复
this.Controls当然不是什么地方都能用
在你的按钮事件中肯定能用
linlinunix 2004-11-22
  • 打赏
  • 举报
回复
添加 this.Controls.Add(button2);就可以了
==========================
没有this.Controls这个东西
这个当然有了在
windows项目中也有你可以看看窗体生成 的函数InitializeComponent()
=============================
halabazi 2004-11-22
  • 打赏
  • 举报
回复
不是呀,我是在windows项目里,不是web项目,没有this.Controls这个东西

InitializeComponent里的我看了,可是我光设好了,就是不知道怎么让它显示出来

System.Windows.Forms.Button button2 = new Button();
button2.Location = new System.Drawing.Point(20, 20);
button2.Name = "button1";
button2.Size = new System.Drawing.Size(20, 20);
button2.TabIndex = 0;
button2.Text = "button2";
flcandclf 2004-11-22
  • 打赏
  • 举报
回复
新建页面,在页面中放入一个button和PlaceHolder控件,双击button控件产生事件,在事件里写如代码如下:
Button mybutton = new Button();
mybutton.Text = "按钮1";
PlaceHolder1.Controls.Add(mybutton);

mybutton = new Button();
mybutton.Text = "按钮2";
PlaceHolder1.Controls.Add(mybutton);
cdo 2004-11-22
  • 打赏
  • 举报
回复
看看楼上两位贴出来的,楼主应该知道怎么做了吧。
一半乐事 2004-11-22
  • 打赏
  • 举报
回复
看看你程序里的private void InitializeComponent()就明白了。
this.button1 = new System.Windows.Forms.Button();
this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button1.Location = new System.Drawing.Point(216, 48);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 22);
this.button1.TabIndex = 5;
this.button1.Text = "浏览";
this.button1.Click += new System.EventHandler(this.button1_Click);
cxyPioneer 2004-11-22
  • 打赏
  • 举报
回复
Button btnExit = new Button();
btnEixt.Text = "退出";
btnExit.Size.X .Y 高度,宽度
btnExit.SetBounds 位置
this.Controls.Add( btnExit );
flyskywlh 2004-11-22
  • 打赏
  • 举报
回复
一楼说了
zhanghw20030303 2004-11-22
  • 打赏
  • 举报
回复
Button button = new Button();
button.text = "Button";
//设置动态信息
this.Controls.Add(button);

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧