C# 向Winform中动态添加的按钮添加事件
我已经在窗口窗体中动态添加了按钮控件,现在我想向每个按钮控件添加不同的事件。 下面是我从数据库中动态添加按钮的代码C# 向Winform中动态添加的按钮添加事件,c#,sql-server,winforms,mouseevent,mousehover,C#,Sql Server,Winforms,Mouseevent,Mousehover,我已经在窗口窗体中动态添加了按钮控件,现在我想向每个按钮控件添加不同的事件。 下面是我从数据库中动态添加按钮的代码 private void GetButtonDynamically() { SqlConnection conn = GetConnection(); conn.Open(); using (conn) { SqlCommand cmd = new SqlCommand("Select
private void GetButtonDynamically()
{
SqlConnection conn = GetConnection();
conn.Open();
using (conn)
{
SqlCommand cmd = new SqlCommand("Select MenuName from tblMainMenu",conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Button mybutton = new Button();
mybutton.Location = new Point(x, y + 54);
y += 54;
mybutton.Height = 44;
mybutton.Width = 231;
mybutton.BackColor = Color.Gainsboro;
mybutton.ForeColor = Color.Black;
mybutton.Text = reader["MenuName"].ToString();
mybutton.Name = reader["MenuName"].ToString();
mybutton.Font = new Font("Georgia", 12);
Controls.Add(mybutton);
mybutton.Click+= new EventHandler(mybutton_Click);
}
conn.Close();
}
}
现在我面临的问题是,它为动态创建的每个按钮生成相同的事件,并且我希望每个按钮使用不同的方法
这里是点击事件
private void mybutton_Click(object sender, EventArgs e)
{
MessageBox.Show("Button is Clicked");
}
我认为它可以帮助您:
private void Form1_Load(object sender, EventArgs e)
{
int y = 0;
for (int i = 0; i < 10; i++)
{
Button mybutton = new Button();
mybutton.Location = new Point(0, y + 10);
y += 54;
mybutton.Height = 44;
mybutton.Width = 231;
mybutton.BackColor = Color.Gainsboro;
mybutton.ForeColor = Color.Black;
mybutton.Text = "a "+i.ToString();
mybutton.Name = "b" + i.ToString();
mybutton.Font = new Font("Georgia", 12);
switch (i)// define your condition
{
case 1:
mybutton.Click += new EventHandler(mybutton_Click);
break;
case 2:
mybutton.Click += new EventHandler(mybutton_1_Click);
break;
default:
break;
}
Controls.Add(mybutton);
}
}
private void mybutton_Click(object sender, EventArgs e)
{
MessageBox.Show("Button 1 is Clicked");
}
private void mybutton_1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button 2 is Clicked");
}
private void Form1\u加载(对象发送方,事件参数e)
{
int y=0;
对于(int i=0;i<10;i++)
{
按钮mybutton=新按钮();
mybutton.Location=新点(0,y+10);
y+=54;
我的按钮。高度=44;
mybutton.宽度=231;
mybutton.BackColor=Color.Gainsboro;
mybutton.ForeColor=颜色.黑色;
mybutton.Text=“a”+i.ToString();
mybutton.Name=“b”+i.ToString();
mybutton.Font=新字体(“Georgia”,12);
开关(i)//定义您的条件
{
案例1:
mybutton.Click+=新建事件处理程序(mybutton\u Click);
打破
案例2:
mybutton.Click+=新建事件处理程序(mybutton\u 1\u Click);
打破
违约:
打破
}
控件。添加(mybutton);
}
}
私有无效mybutton_单击(对象发送者,事件参数e)
{
MessageBox.Show(“单击按钮1”);
}
私有无效mybutton_1_单击(对象发送者,事件参数e)
{
MessageBox.Show(“单击按钮2”);
}
您可以在创建按钮时为按钮添加“AccessibleName”。在您的while循环中,在button click事件中获取可访问的名称,并应用开关大小写或循环来区分它。示例代码
int x = 10; int y = 10;
for (int i = 1; i < 5; i++)
{
Button mybutton = new Button();
mybutton.Location = new Point(x, y + 54);
y += 54;
mybutton.Height = 44;
mybutton.Width = 231;
mybutton.BackColor = Color.Gainsboro;
mybutton.ForeColor = Color.Black;
mybutton.Text = i + "MenuName".ToString();
mybutton.Name = i + "MenuName".ToString();
mybutton.AccessibleName = i.ToString();
mybutton.Font = new Font("Georgia", 12);
Controls.Add(mybutton);
mybutton.Click += new EventHandler(mybutton_Click);
}
mybutton\u Click
方法的签名是什么?每个按钮的不同方法有什么区别?mybutton.MouseHover+=mybutton\u Click
应该可以工作,不需要用塑料袋包装delegate@ChetanRanpariya不同的事件意味着,例如,我希望一个按钮显示按钮被单击,而在另一个按钮上我希望显示按钮被悬停。您应该知道要为哪个按钮处理单击
事件,以及要为哪个按钮处理悬停
事件。然后将这些事件处理程序附加到这些按钮。还要记住,事件处理程序的sender
参数包含其事件发生的按钮,您可以通过以下方式获取该按钮:var b=(button)sender对不起,我没有正确地找到你的问题
private void mybutton_Click(object sender, EventArgs e)
{
Button cb = (Button)sender;
string strName = cb.AccessibleName;
switch (strName)
{
case "1":
MessageBox.Show("Button 1 is Clicked");
break;
case "2":
MessageBox.Show("Button 2 is Clicked");
break;
case "3":
MessageBox.Show("Button 3 is Clicked");
break;
default:
break;
}
}