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集合中,为什么却会出现这种情况呢
解决方案 »
- 如何让一个类保持状态?
- 救命呀。调用excel老是出 异常来自 HRESULT:0x800A03EC错。
- 无聊的小问题
- 写个泛型的小代码,结果发现泛型不能比较,谢谢啦
- string.Compare()能按字符的Ascii码比较吗?
- ISerializable接口实现后,没有被调用,请帮忙看看
- C#" 酷点"软件的效果怎么做出来?圆形界面和桌面中央滚动的效果?
- 关于在datagrid控件里添加dropdownlist(下拉列表)问题
- 怎么知道一个对象已经实例了呢?
- 小问题:怎样把string类型数据转换为int,float等数据呢?
- 怎么把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个不同的位置