在Winform中将事件添加到动态添加的按钮

时间:2018-09-17 07:19:53

标签: c# sql-server winforms mouseevent mousehover

我已经以窗口形式动态添加了按钮控件,现在我想为每个按钮控件添加不同的事件。  这是我从数据库动态添加按钮的代码。

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");
    }

2 个答案:

答案 0 :(得分:1)

您可以在创建按钮时为其添加“ AccessibleName”。在您的情况下,在while循环内,并在按钮单击事件中获取可访问的名称,并应用开关大小写或循环来区分它。示例代码

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;
         }
       }

答案 1 :(得分:0)

我认为它可以为您提供帮助:

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");
            }