I have GroupBox that is populated by Button controls, and a combobox that populates the groupBox based on the selected number in combobox(8, 16, 32).

This is my populateGroupbox method

private void populateGroupbox (GroupBox gBox, int number )
            for (int i = 0; i < number; i++)
                Button btn = new Button();                
                btn.Location = new Point(16, 30 + i*30);                
                btn.Text = (i + 1).ToString();
                btn.Width = 40;

Based on the number param in that method it adds that many buttons.

Now here is my problem, if i select 16 or 32 controls kinda spill out of borders of groupBox, i need to find out a way so that if i select more that 8, controls(button) will display in 2 rows of 8 controls, i don't know how to do that.

Any tips?


How can i use flowpanelLayout in this?

So what you need it to calculate the column that each button would be in, and then the row.

Row is equal to i / 8 (0-7 = 0, 8-15 = 1, etc.) and Column is equal to i % 8. Use these values to calculate your Location point ( like you are doing with the column now, but you'll have to add a calculation for the row).