TableLayoutPanel的Autoscroll属性不起作用

时间:2012-12-24 10:02:50

标签: c# .net winforms tablelayout

我想在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);

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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);

当然你应该有tableLayoutPanel.AutoScroll = true

为了避免烦人的水平滚动条,你应该在桌面布局面板上添加正确的填充:

tableLayoutPanel.Padding = 
     new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);

对于tableLayoutPanel,应禁用UPDATE AutoSize。否则,不会显示滚动 - 表格布局面板将会增长。