Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i create a click event for a dynamic button array?i have found lots of answers for dynamic arrays,but i cant implement it for a button array.can any one help me?following code represent how i have implemented the dynamic button array and a click event for it.but it have run time errors saying,"No overload for btn_Click matches delegate System.EventHandler" please help me!!!

private Button[] bu = new Button[12];
public static int index1;
public int[] ar = new int[4];
public string[] cl1 = new string[4];

index1 = 0;
           
           {
            x = 30;
            for (int i = 0; i < ar.Length; i++)
            {
                bu[index1] = new Button();
                bu[index1].Location = new Point(50, x);
                bu[index1].Size = new Size(75, 20);
                bu[index1].Text = "1";

                cl1[i] = "b" + i;//name the buttons @ the 1st column
                lable[index1] = new Label();
                lable[index1].Location = new Point(75,(x+25));
                lable[index1].Size = new Size(35, 13);
                //lable[index1].Text = Convert.ToString(cl1[i]);
                bu[index1].Name = Convert.ToString(cl1[i]);
                lable[index1].Text = bu[index1].Name;
                x = x + 100;
                index1++;
            }
            this.Controls.AddRange(bu);
            this.Controls.AddRange(lable);

            bu[index1].Click += new EventHandler(btn_Click);
        }


        private void btn_Click()
        {
            MessageBox.Show("You hv cast ur vote for the 1ft preference");
        }
Posted

i have found lots of answers for dynamic arrays,but i cant implement it for a button array.
Who said so? Did you look at this link I shared, here this section: Creating a Common Event Handler for a button array[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Mar-11 2:22am    
You recently referenced this, right? My 5 again.
--SA
Espen Harlinn 24-Mar-11 3:39am    
Nice link, my 5
Hi,
this modified code of yours should work:
C#
private Button[] bu = new Button[12];
public static int index1;
public int[] ar = new int[4];
public string[] cl1 = new string[4];
index1 = 0;
           {
            x = 30;
            for (int i = 0; i < ar.Length; i++)
            {
                bu[index1] = new Button();
                bu[index1].Location = new Point(50, x);
                bu[index1].Size = new Size(75, 20);
                bu[index1].Text = "1";
                bu[index1].Click += new EventHandler(button_Click);
                cl1[i] = "b" + i;//name the buttons @ the 1st column
                lable[index1] = new Label();
                lable[index1].Location = new Point(75,(x+25));
                lable[index1].Size = new Size(35, 13);
                //lable[index1].Text = Convert.ToString(cl1[i]);
                bu[index1].Name = Convert.ToString(cl1[i]);
                lable[index1].Text = bu[index1].Name;
                x = x + 100;
                index1++;
            }
            this.Controls.AddRange(bu);
            this.Controls.AddRange(lable);
        }

        private void button_Click(object sender, EventArgs e)
        {
            MessageBox.Show("You hv cast ur vote for the 1ft preference");
        }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Mar-11 1:54am    
One problem with this is lack of identification of button passed to the handler. Another minor problem: using auto-generated "button_Click" (Microsoft naming conventions broken). As a minimum, should be renamed, ideally -- should be anonymous (which can call some method, but this time with semantically sensible signature). I voted 4.
--SA
Espen Harlinn 24-Mar-11 3:39am    
A nice and simple fix, my 5
Member 7779792 24-Mar-11 3:52am    
thanx alot!!it actually works.
i'm sooo happy.i tried soooo hard to do this part during past few days.no words to admit your help.thanx once again!!!!
JF2015 24-Mar-11 3:54am    
Glad I could help you. Please mark my answer as "Accepted" and vote me a 5. I'd really appreciate that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900