how to execute click event on dynamically created button in c#.net

13,344

you can put all your logic into one handler:

System.Windows.Forms.Button b = new System.Windows.Forms.Button();
b.Click += new EventHandler(b_Click);
//finally insert the button where it needs to be inserted.
...

void b_Click(object sender, EventArgs e)
{
    MessageBox.Show(((System.Windows.Forms.Button)sender).Name + " clicked");
}

To your edit:

You are storing the reference for your button(s) inside the Field btnDynamicButton. Hence it always gets overwritten with the latest button you have created. You should not reference the button by using a field. The sender parameter of the click-handler contains the button element that has been clicked. See the code above: Simple cast sender to Button and you know which button has been clicked:

private void btnclick_Click(object sender, EventArgs e)
{
   Button btn = (Button)sender
   label2.Text = btn.Text;
}
Share:
13,344

Related videos on Youtube

Kashif
Author by

Kashif

Updated on September 15, 2022

Comments

  • Kashif
    Kashif over 1 year

    I am trying to build an app, where user can select category and according to it displays its sub categories , these sub categories are buttons, which are dynamically created.

    Now, as buttons are dynamically created so I am confuse how to write code under button_click event as I dont know how many subcategories are there.

    So is there any way I can execute click event of a particular button , so that I can execute certain commands?

    EDITED

    This is the code that i tried

    Button btnDynamicButton = new Button();
    
    private void btnclick_Click(object sender, EventArgs e)
    {
    label2.Text = btnDynamicButton.Text;
    }
    private void btnappetizer_Click(object sender, EventArgs e)
    {
     groupBox2.Visible =false;
     DataTable dt = new DataTable();
     dt = itemmasterbl.SelectallrecordFromtblItem(btnappetizer.Text);
     for (int i = 0; i < dt.Rows.Count; i++)
     {
      string name = "Appetizer" + DynamicButtonCount;
      Button btnDynamicButton1 = new Button();
      btnDynamicButton1.Name = name;
      btnDynamicButton1.Text = name;
      btnDynamicButton1.Size =
      new System.Drawing.Size(150, 30);
      btnDynamicButton1.Location =
      new System.Drawing.Point(180, DynamicButtonCount * 30);
      btnDynamicButton1.Click +=new EventHandler(btnclick_Click);<br>
      Controls.Add(btnDynamicButton1);
      DynamicButtonCount++;
      btnDynamicButton = btnDynamicButton1;
      }
     }
    

    Once I do this it creates three buttons according to number of values in itemmaster DB under appetizer, but once I click on any of the three buttons the label displays only last buttons text,because in last line I have :

    btnDynamicButton = btnDynamicButton1;

    Which will last buttons infos,but rather I want which ever button I press, label should display respective text. How can I achieve this.

    • Marc Gravell
      Marc Gravell about 11 years
      What UI framework is this? for example, in winforms there is PerformClick(). I also wonder if you mean "how do I subscribe to the Click event?", rather than "execute click event" - in which case you just subscribe something to the Click event after creating the control - newControl.Click += .... However, for complex scenarios you may need to use some reflection
    • Marc Gravell
      Marc Gravell about 11 years
      @Kashif frankly, I suspect you'd do well to have a single fixed handler, and do the voodoo in that single method...