Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys. I am new to windows forms.
I am trying the below scenario in windows forms.

I have a list of IDs. I want to generate buttons depending on the ID count.

Ex :
If I have 10 IDs, then I have to generate 10 buttons during form load.

Thanks in Advance

What I have tried:

I have tried it generating with for each. But didn't work.
Posted
Updated 30-Jan-21 18:28pm

 
Share this answer
 
Start by defining where you want them to go: if you assume a horizontal row, then start at a small offset from the left of the form, and define a spacing between them. Then it's a simple matter of creating the buttons and adding them to the form, along with a handler for the Click event:
C#
private void MyForm_Load(object sender, EventArgs e)
    {
    int x = 20;
    int y = 20;
    int horizontalSpacing = 100;
    Size s = new Size(75, 23);
    for (int i = 1; i <=5; i++)
        {
        Button b = new Button() { Location = new Point(x, y), Text = i.ToString(), Size = s};
        b.Click += MyButton_Click;
        Controls.Add(b);
        x += horizontalSpacing;
        }
    }
 
Share this answer
 
Thank you so much. It worked !
 
Share this answer
 

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