多按钮触发事件

发布时间 2023-12-17 10:17:20作者: 楠哥x

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();


//foreach (Control ctl in this.Controls)
//{
// if (ctl is Button)
// {

// ctl.Click += new System.EventHandler(this.button_Click);

// }
//}
foreach(Control cl in this.Controls )
{
if (cl is Button)
{
cl.Click += Cl_Click;
}
}

}

private void Cl_Click(object sender, EventArgs e)
{
Button btn = (Button )sender ;
MessageBox.Show(btn.Text );
}

 

private void button11_Click(object sender, EventArgs e)
{

}
}
}