将事件添加到 Winform 中动态添加的按钮
Add Events To Dynamically added Buttons in Winform
我已经在 window 表单中动态添加了按钮控件,现在我想为每个按钮控件添加不同的事件。
这是我从数据库动态添加按钮的代码。
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");
}
您可以在创建按钮时为您的按钮添加一个 "AccessibleName"。在您的 while 循环中的情况下,在按钮单击事件中获取可访问的名称并应用 switch case 或循环来区分它。示例代码
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);
}
在按钮中点击修改成这样
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;
}
}
我已经在 window 表单中动态添加了按钮控件,现在我想为每个按钮控件添加不同的事件。 这是我从数据库动态添加按钮的代码。
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");
}
您可以在创建按钮时为您的按钮添加一个 "AccessibleName"。在您的 while 循环中的情况下,在按钮单击事件中获取可访问的名称并应用 switch case 或循环来区分它。示例代码
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);
}
在按钮中点击修改成这样
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;
}
}