带有 FlowLayouts 的 TableLayoutPanel 中的 C# ScrollBar
C# ScrollBar in TableLayoutPanel with FlowLayouts
我有一行六列的 TableLayoutPanel。我正在为每一列添加一个 FlowLayoutPanel。在 FlowLayoutPanels 中,我正在添加动态控件。
我无法将控件直接添加到 TableLayoutPanel,因为添加和删除对于许多行而言太慢(从行中删除控件,然后将控件从下一行的行向上移动)。
但是当我向 FlowLayoutPanel 添加控件时,TableLayoutPanel 不显示 ScrollBar。
我有的是:
TableLayoutPanel: 1 行,6 列,DockStyle:Fill,AutoScroll:True
FlowLayoutPanel: DockStyle:Fill, FlowDirection:TopDown, WrapContents:False
ScrollBar 必须显示在 TableLayoutPanel 中。
示例代码(我认为它没有帮助,其余设置为可视模式):
// 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 );
还有图片,如你所见,左侧没有滚动条,但控件隐藏在最后(TableLayoutPanel 是带边框的控件):
感谢Reza Aghaei,我是这样解决的:
我创建了 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 );
}
此代码在 FlowLayoutPanel
中创建 10 行。
我有一行六列的 TableLayoutPanel。我正在为每一列添加一个 FlowLayoutPanel。在 FlowLayoutPanels 中,我正在添加动态控件。
我无法将控件直接添加到 TableLayoutPanel,因为添加和删除对于许多行而言太慢(从行中删除控件,然后将控件从下一行的行向上移动)。
但是当我向 FlowLayoutPanel 添加控件时,TableLayoutPanel 不显示 ScrollBar。
我有的是:
TableLayoutPanel: 1 行,6 列,DockStyle:Fill,AutoScroll:True
FlowLayoutPanel: DockStyle:Fill, FlowDirection:TopDown, WrapContents:False
ScrollBar 必须显示在 TableLayoutPanel 中。
示例代码(我认为它没有帮助,其余设置为可视模式):
// 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 );
还有图片,如你所见,左侧没有滚动条,但控件隐藏在最后(TableLayoutPanel 是带边框的控件):
感谢Reza Aghaei,我是这样解决的:
我创建了 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 );
}
此代码在 FlowLayoutPanel
中创建 10 行。