我被一个 winform 挑战困住了。我试图做一个非常简单的布局,但我没有找到好的和动态的解决方案。
事情是:我想制作一个布局,其中有一个标签,然后是 1 到 n 个按钮。如果按钮不合时宜(右侧),按钮应在新行上形成自己。在最后一个按钮之后,在新行上,布局可以重复。
如果控件的数量超过了表单的高度,我希望布局可以制作一个垂直滚动条。
我的最新方法是有一个两列的 TableLayoutPanel,第 0 列应该有标签,第 1 列应该有一个 FlowLayoutPanel。但是现在不管如何设置布局无法按预期工作。
试图在这里说明:
| Label1 | Button1 Button2 Button3 Button4 Button5 |
| | Button6 |
--------------------------------------------------------
| Label2 | Button1 Button2 Button3 Button4 Button5 |
等等。
(不用说行和按钮的数量取决于基础数据,并且控件将以编程方式生成。
谁能给我一个提示?
我设法使用您最后一种方法中提到的方法获得了您想要的行为:
向 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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句