Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

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;
           // ...
        }
     }
  }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #add #dynamically #buttons #loop #events #winform
ADD COMMENT
Topic
Name
5+1 =