Align controls to center in a FlowLayout

后端 未结 7 887
一向
一向 2020-12-30 01:47

I have a flowlayout as follows:

\"enter

I need to center all the controls on t

7条回答
  •  忘掉有多难
    2020-12-30 02:46

    I'd go with TableLayoutPanel instead:

    • Put TableLayoutPanel on your form
    • Set dock style Fill to panel
    • Leave only one column inside panel
    • Create row for every button (and put buttons to table cells)
    • Set row size type Autosize
    • Set dock style Fill to every button, except last one
    • Set dock style Top to last button

    BTW in your solution you should iterate over flowLayoutPanel controls instead of form controls. Also consider subtracting horizontal margin and padding from width:

    foreach (Control control in flowLayoutPanel.Controls)
    {
        control.Size = new Size(flowLayoutPanel.Width - control.Margin.Horizontal,
                                control.Height); 
    }
    

    But I advise you to use TableLayoutPanel instead.

提交回复
热议问题