How to add buttons dynamically to my form?

前端 未结 8 1888
盖世英雄少女心
盖世英雄少女心 2020-12-01 12:34

I want to create 10 buttons on my form when I click on button1. No error with this code below but it doesnt work either.

private void button1_Click(object se         


        
8条回答
  •  臣服心动
    2020-12-01 13:08

    You could do something like this:

    Point newLoc = new Point(5,5); // Set whatever you want for initial location
    for(int i=0; i < 10; ++i)
    {
        Button b = new Button();
        b.Size = new Size(10, 50);
        b.Location = newLoc;
        newLoc.Offset(0, b.Height + 5);
        Controls.Add(b);
    }
    

    If you want them to layout in any sort of reasonable fashion it would be better to add them to one of the layout panels (i.e. FlowLayoutPanel) or to align them yourself.

提交回复
热议问题