private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 300; i += 100)
{
Button button2 = new Button();
button2.Location = new Point(i, 100);
this.Controls.Add(button2);
button2.Text = "aa";
}
}
这样添加的三个按钮为什么不冲突呢 但是我在窗体里面添加三个按钮都叫button2 就会冲突,最后都是添加到Controls集合中,为什么却会出现这种情况呢
{
for (int i = 0; i < 300; i += 100)
{
Button button2 = new Button();
button2.Location = new Point(i, 100);
this.Controls.Add(button2);
button2.Text = "aa";
}
}
这样添加的三个按钮为什么不冲突呢 但是我在窗体里面添加三个按钮都叫button2 就会冲突,最后都是添加到Controls集合中,为什么却会出现这种情况呢
解决方案 »
- 如何对数据进行去重处理
- 请教:c#中怎么使用命名锁
- 用c#写仿microsoft记事本,在richtextbox怎样统一字体?
- 关于remoting中如何插入自定义的sink(急啊)
- pop3的问题
- 在线急等关于读html的问题
- 怎样用nunit调使用leSystem.Windows.Forms.WebBrowser的winform程序
- 在C#的OWC编程中怎么把记录集数据赋给数据透视表,类型要怎么样转换!
- 怎么样才能给代码生成的ListViewItem加入Tooltips呢?
- 求教这句话的意思,为什么这么写
- 怎么把GridView的数据一键上传到数据库中?求代码 在线
- C# winform 第三方显示数据的控件
button2.Location = new Point(i, 100);
this.Controls.Add(button2);
button2.Text = "aa";
MessageBox.Show(button2.Name);
MessageBox.Show(button2.Name);
Application.DoEvents();
我突然领悟到学习编程是需要想的!
想!学习.net这么多年,我居然大部分时间都忘了“想”!
啊~~谢谢楼主~~谢谢楼主~~愿主保佑你!
button1.Location = new Point(100, 120);
this.Controls.Add(button1);
button1 = new Button();
button1.Location = new Point(200, 120);
this.Controls.Add(button1);
button1 = new Button();
button1.Location = new Point(300, 120);
this.Controls.Add(button1);变量你使用几次都没问题,但是声明只能声明一次,这么说能明白么?
你在窗体里托一个button,把Nmae属性命名为“btn1”再托一个button,把name属性也命名为“btn1”,此时会出错……事实证明同一容器里两个控件的名字不能重复,否则在代码里使用btn1,程序不知道你是用的第一个button还是第二个button
你是实例化了button控件,但是这个button的id并不是button2,button2只不过是个对象名,实在不明白,想想类的实例化
for (int i = 0; i < 300; i += 100)
{
Button button2 = new Button();
button2.Name = "test";
button2.Location = new Point(i, 100);
this.Controls.Add(button2);
button2.Text = "aa";
}
for (int i = 0; i < this.Controls.Count; i++)
{
Button bt = this.Controls[i] as Button;
MessageBox.Show(bt.Name +";"+ bt.ToString());
}
//那3个其实是一个按钮,只是被拿出来用了3次,放在3个不同的位置