Search code examples
c#sql-serverwinformsmouseeventmousehover

Add Events To Dynamically added Buttons in Winform


I had Added Button Controls dynamically in window form,now i want to add different event to every button control. Here is my code of adding button dynamically from database.

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

Now the problem i am facing is it generate same Event for every button that is created dynamically, and i want different method for every button

Here Is Click Event

 private void mybutton_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Button is Clicked");
    }

Solution

  • You can add a "AccessibleName" for your button while creating it. In your case inside your while loop and get the accessible name in button click event and apply a switch case or loop to differentiate it. sample code

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

    In Button click modify like this

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