我想在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);
答案 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
。否则,不会显示滚动 - 表格布局面板将会增长。