直接把前一个按钮对象保存在当前按钮的Tag属性里面:
private void Form2_Load(object sender, EventArgs e){
Button btn1=null//前一个按钮
for(int i=1i<21i++){
Button btn = new Button()
btn.Text = "button" + i
btn.Name = "button" + i
btn.Click += new EventHandler(btn_Click)
btn.Size = new System.Drawing.Size(80,20)
btn.Tag = btn1
btn.Location = new Point(i * 30, i * 20)
this.Controls.Add(btn)
btn1 = btn
}
}
void btn_Click(object sender, EventArgs e) {
Button btn = sender as Button
MessageBox.Show(btn.Name.ToString())
if (btn.Tag != null)
{
//d出前一个按钮的位置信息
Button btn1 = btn.Tag as Button
MessageBox.Show("X:"+btn1.Location.X +" Y:"+ btn1.Location.X)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)