简体   繁体   English

具有FlowLayouts的TableLayoutPanel中的C#ScrollBar

[英]C# ScrollBar in TableLayoutPanel with FlowLayouts

I have TableLayoutPanel with one row and six columns. 我有一排六列的TableLayoutPanel。 I'm adding for each column one FlowLayoutPanel. 我为每列添加一个FlowLayoutPanel。 In FlowLayoutPanels I'm adding dynamically controls. 在FlowLayoutPanels中,我添加了动态控件。
I can't add controls directly to TableLayoutPanel becouse adding and deleting is too slow for many rows (removing controls from row and then moving controls from rows below one row up). 我无法将控件直接添加到TableLayoutPanel,因为添加和删除对于许多行来说太慢(从行中删除控件,然后从一行以下的行中删除控件)。
But when I'm adding controls to FlowLayoutPanel, TableLayoutPanel doesn't show ScrollBar. 但是,当我向FlowLayoutPanel添加控件时,TableLayoutPanel不会显示ScrollBar。

What I have is: 我所拥有的是:
TableLayoutPanel: 1 row, 6 cols, DockStyle:Fill, AutoScroll:True TableLayoutPanel: 1行,6列,DockStyle:填充,AutoScroll:True
FlowLayoutPanel: DockStyle:Fill, FlowDirection:TopDown, WrapContents:False FlowLayoutPanel: DockStyle:填充,FlowDirection:TopDown,WrapContents:False

ScrollBar must show in TableLayoutPanel. ScrollBar必须显示在TableLayoutPanel中。

Sample code (I don't think that it helps, rest is set in visual mode): 示例代码(我认为这没有帮助,其余设置为可视模式):

// create controls
CheckBox control1 = new CheckBox();
ComboBox control2 = new ComboBox();
ComboBox control3 = new ComboBox();
ComboBox control4 = new ComboBox();
ComboBox control5 = new ComboBox();
CheckBox control6 = new CheckBox();

control1.AutoSize   = false;
control1.CheckAlign = System.Drawing.ContentAlignment.MiddleCenter;

control3.DropDownStyle = ComboBoxStyle.DropDownList;
control3.Enabled       = false;

control4.DropDownStyle = ComboBoxStyle.DropDownList;
control4.Enabled       = false;

control5.DropDownStyle = ComboBoxStyle.DropDownList;
control5.Enabled       = false;

control6.AutoSize   = false;
control6.CheckAlign = System.Drawing.ContentAlignment.MiddleCenter;
control6.Enabled    = false;

// add to float layout panels
this.flayControl1.Controls.Add( control1 );
this.flayControl2.Controls.Add( control2 );
this.flayControl3.Controls.Add( control3 );
this.flayControl4.Controls.Add( control4 );
this.flayControl5.Controls.Add( control5 );
this.flayControl6.Controls.Add( control6 );

And image, as you see, no scrollbar on left, but controls are hiding at the end (TableLayoutPanel is control with borders): 如您所见,图像左侧没有滚动条,但是控件最后隐藏了(TableLayoutPanel是带有边框的控件): 在此处输入图片说明

Thanks to Reza Aghaei , I solved it by this way: 感谢Reza Aghaei ,我通过以下方式解决了它:

I created FlowLayoutPanel and then, I created TableLayoutPanel for each row in FlowLayoutPanel . 我创建了FlowLayoutPanel ,然后为FlowLayoutPanel每一行创建了TableLayoutPanel

int pad1, pad2, height = 28;

FlowLayoutPanel panel = new FlowLayoutPanel();
panel.AutoScroll = true;
panel.FlowDirection = FlowDirection.TopDown;
panel.WrapContents = false;

for( int x = 0; x < 10; ++x )
{
    TableLayoutPanel table = new TableLayoutPanel();
    table.Width = panel.Width;

    // controls
    CheckBox control1 = new CheckBox();
    ComboBox control2 = new ComboBox();
    ComboBox control3 = new ComboBox();
    TextBox  control4 = new TextBox();
    TextBox  control5 = new TextBox();
    CheckBox control6 = new CheckBox();

    // columns and rows number
    table.ColumnCount = 6;
    table.RowCount    = 1;

    // row height
    table.RowStyles.Add( new RowStyle(SizeType.Absolute, height) );

    // widths of columns
    table.ColumnStyles.Add( new ColumnStyle(SizeType.Percent, 5.0f ) );
    table.ColumnStyles.Add( new ColumnStyle(SizeType.Percent, 24.0f) );
    table.ColumnStyles.Add( new ColumnStyle(SizeType.Percent, 20.0f) );
    table.ColumnStyles.Add( new ColumnStyle(SizeType.Percent, 22.0f) );
    table.ColumnStyles.Add( new ColumnStyle(SizeType.Percent, 24.0f) );
    table.ColumnStyles.Add( new ColumnStyle(SizeType.Percent, 5.0f ) );

    // fill
    control1.Dock = control2.Dock = control3.Dock = control4.Dock =
    control5.Dock = control6.Dock = DockStyle.Fill;

    // margin
    pad1 = (height - control2.Height) / 2;
    pad2 = height - control2.Height - pad1;

    control2.Margin = control3.Margin = new Padding( 2, pad1, 2, pad2 );

    // textboxes margin
    pad1 = (height - control4.Height) / 2;
    pad2 = height - control4.Height - pad1;

    control4.Margin = control5.Margin = new Padding( 2, pad1, 2, pad2 );

    // add elements to TableLayoutPanel
    table.Controls.Add( control1, 0, 0 );
    table.Controls.Add( control2, 1, 0 );
    table.Controls.Add( control3, 2, 0 );
    table.Controls.Add( control4, 3, 0 );
    table.Controls.Add( control5, 4, 0 );
    table.Controls.Add( control6, 5, 0 );

    // add table to FlowLayoutPanel
    panel.Controls.Add( table );
}

This code creates 10 rows in FlowLayoutPanel . 此代码在FlowLayoutPanel创建10行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM