本文介绍了从事件处理程序获取表单中的动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题是如何在下面的代码中使用btncki_click中的txtcki文本:
the problem is how can i use txtcki text in btncki_click in below code:
public Form2()
{
int xllngth,loc=20;
for (; ; xllngth++)
{
var buncoockie = new System.Windows.Forms.Button();
buncoockie.Name = "btncki" + xllngth;
buncoockie.Text ="cki"+ Convert.ToString(sheet1.Cells[xllngth, 2].value);
buncoockie.Size = new Size(100, 20);
buncoockie.Location = new System.Drawing.Point(145, loc);
buncoockie.BackColor = Color.AliceBlue;
buncoockie.Click += Btncki_Click;
this.Controls.Add(buncoockie);
var txtcki=new System.Windows.Forms.TextBox();
txtcki.Name = "txtcki" + xllngth;
txtcki.Size = new Size(150, 20);
txtcki.Location = new System.Drawing.Point(245, loc);
this.Controls.Add(txtcki);
loc += 20;
}
}
private void Btncki_Click(object sender, EventArgs e)
{
//string temp=txtcki.text;
}
推荐答案
var buncoockie = new System.Windows.Forms.Button();
buncoockie.Name = "btncki" + xllngth;
buncoockie.Text = "cki" + Convert.ToString(sheet1.Cells[xllngth, 2].value);
buncoockie.Size = new Size(100, 20);
buncoockie.Location = new System.Drawing.Point(145, loc);
buncoockie.BackColor = Color.AliceBlue;
buncoockie.Click += Btncki_Click;
var txtcki = new System.Windows.Forms.TextBox();
txtcki.Name = "txtcki" + xllngth;
txtcki.Size = new Size(150, 20);
txtcki.Location = new System.Drawing.Point(245, loc);
buncoockie.Tag = txtcki;
this.Controls.Add(buncoockie);
this.Controls.Add(txtcki);
loc += 20;
然后,在事件处理程序中:
Then, in the event handler:
private void Btncki_Click(object sender, EventArgs e)
{
Button buncoockie = sender as Button;
if (buncoockie != null)
{
TextBox txtcki = buncoockie.Tag as TextBox;
if (txtcki != null)
{
string temp = txtcki.text;
...
}
}
}
这篇关于从事件处理程序获取表单中的动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!