add dynamically buttons in loop with events winform c#

43

add dynamically buttons in loop with events winform c# -

for (int i = 0; i < 5; i++)
     {
        Button button = new Button();
        button.Location = new Point(160, 30 * i + 10);
        button.Click += new EventHandler(ButtonClickOneEvent);
        button.Tag = i;
        this.Controls.Add(button);
     }

void ButtonClickOneEvent(object sender, EventArgs e)
  {
     Button button = sender as Button;
     if (button != null)
     {
        // now you know the button that was clicked
        switch ((int)button.Tag)
        {
           case 0:
              // First Button Clicked
              break;
           case 1:
              // Second Button Clicked
              break;
           // ...
        }
     }
  }

Comments

Submit
0 Comments