Reputation: 131
I'm stuck with a winform challange. Im trying to make a pretty simple layout, yet I fail to find the good and dynamic solution.
Thing is: I would like to make a layout where we have a label, followed by 1 to n buttons. In case the buttons goes out of form (right side) the buttons should form themself on a new line. After the last button, on a new line, the layout could repeat.
In case the amount of controls exceed the height of the form, I expect the layout to make a vertical scrollbar.
My latest approach is to have a TableLayoutPanel with two columns, column 0 should have the label, column 1 should have a FlowLayoutPanel. But now matter how a set the properties the layout fails to work as expected.
Trying to illustrate here:
| Label1 | Button1 Button2 Button3 Button4 Button5 |
| | Button6 |
--------------------------------------------------------
| Label2 | Button1 Button2 Button3 Button4 Button5 |
and so forth.
(needless to say the amount of rows and buttons are depending underlying data, and the controls will be generated programmatic.
Can anyone giveme a hint?
Upvotes: 0
Views: 51
Reputation: 952
I managed to get your desired behaviour with the method mentioned in your last approach:
Add rows to the TableLayoutPanel:
void AddRow(string labelText, Button[] buttons)
{
RowStyle temp = tableLayoutPanel.RowStyles[tableLayoutPanel.RowCount - 1]; //deafult row style
tableLayoutPanel.RowCount++;
tableLayoutPanel.RowStyles.Insert(tableLayoutPanel.RowCount - 2, new RowStyle(temp.SizeType, temp.Height)); //insert infront of the default row
FlowLayoutPanel flowPanel = new FlowLayoutPanel()
{
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
Dock = DockStyle.Fill
};
flowPanel.Controls.AddRange(buttons); //Add the buttons to the flowpanel
Label label = new Label()
{
Text = labelText,
Dock = DockStyle.Fill
};
//Insert the controls to the tableLayout
tableLayoutPanel.Controls.Add(label, 0, tableLayoutPanel.RowCount - 2);
tableLayoutPanel.Controls.Add(flowPanel, 1, tableLayoutPanel.RowCount - 2);
}
Upvotes: 1