TableLayoutPanel的Autoscroll属性不起作用

Osa*_*mad 3 .net c# tablelayout winforms

我想TableLayoutPanel在GUI上的固定区域中动态添加行.所以,如果记录数量增加,那么我想要一个垂直滚动条,帮助用户查看更多记录.为此,我设置了Property AutoScroll = true;但它不起作用.

CheckBox c = new CheckBox();
c.Text = "Han";
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
this.tableLayoutPanel1.RowCount = 1; this.tableLayoutPanel1.Controls.Add(c, 0, 0);
tableLayoutPanel1.AutoScrollPosition = new Point(0, tableLayoutPanel1.VerticalScroll.Maximum);
this.tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
Run Code Online (Sandbox Code Playgroud)

xeo*_*111 7

从另一个问题的评论中查看代码,您似乎在每行添加行样式,尝试添加行而不添加样式或首先添加一个样式然后添加所有行.

  tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));

            this.tableLayoutPanel1.Controls.Add(c);
            this.tableLayoutPanel1.Controls.Add(c1);
            this.tableLayoutPanel1.Controls.Add(c2);
tableLayoutPanel1.VerticalScroll.Maximum = 200;
            this.tableLayoutPanel1.AutoScroll = true;
Run Code Online (Sandbox Code Playgroud)


Ser*_*kiy 5

因此你没有发布你的代码,我不能说你做错了什么.但这是您应该向表格布局面板添加控件的方式:

tableLayoutPanel.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.RowCount = tableLayoutPanel.RowStyles.Count;
YourCountrol control = new YourControl();
// setup your control properties
tableLayoutPanel.Controls.Add(control);
// scroll to the bottom to see just added control
tableLayoutPanel.AutoScrollPosition = 
    new Point(0, tableLayoutPanel.VerticalScroll.Maximum);
Run Code Online (Sandbox Code Playgroud)

当然你应该有 tableLayoutPanel.AutoScroll = true

顺便说一句,为避免恼人的水平滚动条,您应该在表布局面板中添加正确的填充:

tableLayoutPanel.Padding = 
     new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
Run Code Online (Sandbox Code Playgroud)

AutoSize应该为tableLayoutPanel禁用UPDATE .否则不会出现滚动 - 表格布局面板将会增长.