我想从另一个用户生成一个userControl,在准备好该UserControl接收数据之后,我正努力传递一个int值来知道单击了哪个Card来使用它的数据。
我知道以下代码为假,我需要一个解决方案,请帮忙
private BunifuThinButton2 generatetAributeStandBtn(int i)
{
BunifuThinButton2 button = new BunifuThinButton2();
button.Name = "attributeBtn_" + i.ToString();
button.Click += new EventHandler(this.button_Click);
button.Size = new Size(114, 39);
button.Location = new Point(30,150);
return button;
}
private void button_Click(object sender, System.EventArgs e, int i)
{
Console.WriteLine(" Parents Name" +);
//actualModal = new confirmStandsModal();
//this.Controls.Add(actualModal);
}[Code Screen ][1]
答案 0 :(得分:1)
sender
参数是触发事件的Control
实例,在您的情况下是BunifuThinButton2
实例。因此,您可以在该类中创建一个自定义文件(或先派生一个自定义类),也可以尝试从Name
属性中提取ID。
private void button_Click(object sender, System.EventArgs e)
{
var btn = sender as BunifuThinButton2;
var id = int.Parse(btn.Name.Split(new [] {'_'})[1]);
}