winforms中的动态表格布局

永远的菜鸟

我被一个 winform 挑战困住了。我试图做一个非常简单的布局,但我没有找到好的和动态的解决方案。

事情是:我想制作一个布局,其中有一个标签,然后是 1 到 n 个按钮。如果按钮不合时宜(右侧),按钮应在新行上形成自己。在最后一个按钮之后,在新行上,布局可以重复。

如果控件的数量超过了表单的高度,我希望布局可以制作一个垂直滚动条。

我的最新方法是有一个两列的 TableLayoutPanel,第 0 列应该有标签,第 1 列应该有一个 FlowLayoutPanel。但是现在不管如何设置布局无法按预期工作。

试图在这里说明:

|  Label1  |  Button1 Button2 Button3 Button4 Button5  |
|          |  Button6                                  |
--------------------------------------------------------
|  Label2  |  Button1 Button2 Button3 Button4 Button5  |

等等。

(不用说行和按钮的数量取决于基础数据,并且控件将以编程方式生成。

谁能给我一个提示?

海鲁斯

我设法使用您最后一种方法中提到的方法获得了您想要的行为:

  1. 创建一个 TableLayoutPanel
  2. 将 AutoScroll 属性设置为 true
  3. 将 2 列添加到 TableLayoutPanel
  4. 将两列(或至少最后一列)的 SizeType 属性设置为 AutoSize
  5. 将默认行的 SizeType 属性设置为 AutoSize
  6. 向 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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在表格布局中动态插入相对布局

来自分类Dev

动态设置表格布局

来自分类Dev

动态设置表格布局

来自分类Dev

Android填充表格具有动态布局

来自分类Dev

高图表动态高度与表格布局

来自分类Dev

winforms中表格布局的可滚动背景图像

来自分类Dev

Android中的表格/列表布局

来自分类Dev

动态布局中的图像对齐

来自分类Dev

约束布局中的动态视图

来自分类Dev

在表格中动态创建列

来自分类Dev

更改表格行,CustomCell和布局的高度动态

来自分类Dev

不同表格视图部分中的不同布局

来自分类Dev

在表格html列中设置文本布局

来自分类Dev

TextView在表格布局中未正确显示

来自分类Dev

表格布局中不需要的行

来自分类Dev

允许元素在表格中溢出表格布局:固定

来自分类Dev

动态创建表格布局,该布局可根据屏幕尺寸自动适合单元格。

来自分类Dev

布局中的CakePHP动态菜单内容

来自分类Dev

在onCreateView()片段中设置动态布局

来自分类Dev

在scrollView中的相对布局之间动态滚动

来自分类Dev

在布局ANDROID中动态添加TextView

来自分类Dev

在Eclipse中,以动态布局弹出的文本链接

来自分类Dev

在布局中动态显示图像

来自分类Dev

在onCreateView()片段中设置动态布局

来自分类Dev

在线性布局中添加动态内容

来自分类Dev

布局中的CakePHP动态菜单内容

来自分类Dev

使用JSON响应中的动态布局:angularJS

来自分类Dev

在自动布局中动态更改UIScrollView的大小

来自分类Dev

在布局与 ListView 中动态添加视图的性能