我正在创建一个 C# 应用程序,它从数据库中获取数据,并在一行中动态创建 5 个文本框和一个按钮。
数据库中存在的行数等于创建的文本框和按钮的行数。
我可以成功地创建文本框和按钮的行,文本框甚至能够显示从数据库中获取的数据。
然而,我的问题是生成的按钮在单击时什么也不做,现在这并不意外,因为我还没有创建处理程序来处理单击事件。但我对如何为再次动态生成的按钮动态创建点击事件处理程序感到困惑。
下面是生成文本框和按钮的代码示例。
for (int i = 3; i <= count; i++)
{
com.Parameters[0].Value = i;
using (SqlCeDataReader rd = com.ExecuteReader())
if (rd.Read())
{
pname = (rd["pname"].ToString());
cname = (rd["cname"].ToString());
budget = (rd["budget"].ToString());
advance = (rd["advance"].ToString());
ddate = (rd["ddate"].ToString());
TextBox tobj = new TextBox();
tobj.Location = new Point(10, (40 + ((i - 2) * 20)));
tobj.Tag = 1;
tobj.Text = pname;
tobj.AutoSize = false;
tobj.Width = 150;
tobj.ReadOnly = true;
this.Controls.Add(tobj);
TextBox tobj1 = new TextBox();
tobj1.Location = new Point(160, (40 + ((i - 2) * 20)));
tobj1.Tag = 2;
tobj1.Text = cname;
tobj1.AutoSize = false;
tobj1.Width = 150;
tobj1.ReadOnly = true;
this.Controls.Add(tobj1);
TextBox tobj2 = new TextBox();
tobj2.Location = new Point(310, (40 + ((i - 2) * 20)));
tobj2.Tag = 3;
tobj2.Text = budget;
tobj2.AutoSize = false;
tobj2.Width = 100;
tobj2.ReadOnly = true;
this.Controls.Add(tobj2);
TextBox tobj3 = new TextBox();
tobj3.Location = new Point(410, (40 + ((i - 2) * 20)));
tobj3.Tag = 4;
tobj3.Text = advance;
tobj3.AutoSize = false;
tobj3.Width = 100;
tobj3.ReadOnly = true;
this.Controls.Add(tobj3);
TextBox tobj4 = new TextBox();
tobj4.Location = new Point(510, (40 + ((i - 2) * 20)));
tobj4.Tag = 5;
tobj4.Text = ddate;
tobj4.AutoSize = false;
tobj4.Width = 100;
tobj4.ReadOnly = true;
int due = 0;
due = int.Parse(ddate);
if (due < 5)
{
tobj4.BackColor = System.Drawing.Color.Red;
}
this.Controls.Add(tobj4);
Button button = new Button();
button.Left = 620;
button.Tag = i;
button.Height = 20;
button.Text = "Details";
button.Top = (40 + ((i - 2) * 20));
this.Controls.Add(button);
}
}
请给我一些关于如何生成点击事件处理程序的想法。