我有SQL的流程布局面板,我知道如何将项目从SQL加载到流程布局面板作为按钮,但现在我需要你的帮助每个按钮都有click事件那么如何控制我加载的每个Button的事件它
items_Panles.Controls.Clear();
SqlConnection con = new SqlConnection("Data Source=DESKTOP-6HNIPQ5;Initial Catalog=Anass;Persist Security Info=True;User ID=sa;Password=123");
SqlDataAdapter sda = new SqlDataAdapter("select * from Table_Employee", con);
DataTable dt = new DataTable();
sda.Fill(dt);
for(int i=0;i<dt.Rows.Count;i++)
{
Button btn = new Button();
btn.Name = "btn" + dt.Rows[i][0].ToString();
btn.Text = dt.Rows[i][1].ToString();
btn.Height = 80;
btn.Width = 75;
items_Panles.Controls.Add(btn);
}
单击“个人”按钮
时,此代码会激活答案 0 :(得分:0)
找出你的点击方法,例如:
void btn_Click(object sender, EventArgs e) {
Button b = sender as Button;
if (b != null) {
MessageBox.Show(b.Name);
}
}
然后在创建按钮时,附加处理程序:
Button btn = new Button();
btn.Click += btn_Click;