📌  相关文章
📜  处理多个按钮的点击事件 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:06.295000             🧑  作者: Mango

代码示例1
private int counter=0;

    private void CreateButton_Click(object sender, EventArgs e)
    {
        //Create new button.
        Button button = new Button();

        //Set name for a button to recognize it later.
        button.Name = "Butt"+counter;

       // you can added other attribute here.
        button.Text = "New";
        button.Location = new Point(70,70);
        button.Size = new Size(100, 100);

       // Increase counter for adding new button later.
        counter++;

        // add click event to the button.
        button.Click += new EventHandler(NewButton_Click);
   }

    // In event method.
    private void NewButton_Click(object sender, EventArgs e)
    {
        Button btn = (Button) sender; 

        for (int i = 0; i < counter; i++)
        {
            if (btn.Name == ("Butt" + i))
            {
                // When find specific button do what do you want.
                //Then exit from loop by break.
                break;
            }
        }
    }