I'm working on C# .NET 3.5 desktop application. In the software I have a Form
, which contains a TableLayoutPanel
tp1
. The 2nd row of the tp1
contains a GroupBox
. The GroupBox
contains another TableLayoutPanel
tp2
. tp2
will grow dynamically during runtime.
Form
-----------------------------
| TableLayoutPanel tp1 |
|____________________________|
| GroupBox |
| ________________________|
| | |
| | TableLayoutPanel tp2 |
| | ______________________|
| | | |
| | | Dynamic content |
| | | here |
| | | |
| | |______________________|
| |________________________|
| |
|____________________________|
-----------------------------
The AutoScroll of the Form
is true.
The SizeType of Row 2 of tp1
is AutoSize.
The AutoSize of the GroupBox
is true.
The AutoSize of tp2
is true. tp2
is dynamically updated by the following code:
tp2.RowCount = tp2.RowCount + 1;
tp2.RowStyles.Add(new RowStyle(SizeType.AutoSize));
GroupBox gb = new GroupBox();
gb.BackColor = Color.Red;
tp2.Controls.Add(gb, 0, tp2.RowCount - 1);
I want a scrollbars to appear in the Form
when tp1
is larger than the window size. But no scrollbar is appearing.
I can activate scrollbars in the same way if I set AutoScroll to true for tp1
. But I want the scrollbars on the Form
, not tp1
.
How can I achieve that?
答案 0 :(得分:2)
您需要使用设计器或代码执行此类设置:
var panel1 = new TableLayoutPanel();
panel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
panel1.RowCount = 2;
panel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 20));
panel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
panel1.AutoSize = true;
panel1.AutoSizeMode= System.Windows.Forms.AutoSizeMode.GrowAndShrink;
var groupBox1 = new GroupBox() { Text = "GroupBox" };
groupBox1.AutoSize = true;
groupBox1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
var panel2 = new TableLayoutPanel() {Top= 24, Left= 5 };
panel2.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
panel2.AutoSize = true;
panel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
groupBox1.Controls.Add(panel2);
panel1.Controls.Add(new Label() { Text = "Label" });
panel1.Controls.Add(groupBox1);
this.SuspendLayout();
this.Controls.Add(panel1);
this.AutoScroll = true;
for (int i = 0; i < 10; i++)
{
panel2.RowCount += 1;
panel2.RowStyles.Add(new RowStyle(SizeType.AutoSize));
panel2.Controls.Add(new GroupBox()
{
Text = string.Format("GroupBox{0}", i + 1)
});
}
this.ResumeLayout(true);
这就是结果: