如何将第三个参数传递给EventHandler()

时间:2019-05-30 14:10:54

标签: c# onclick controls eventhandler

我想从另一个用户生成一个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]

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]);
}