Reputation: 129
I wanted to add rows dynamically in TableLayoutPanel
with in a fixed area on GUI. So, if the number of records increases then I want a vertical scroll bar that will help user to see more records. For this purpose, I set PropertyAutoScroll = true;
but it is not working.
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);
Upvotes: 4
Views: 12902
Reputation: 1045
Looking at your code from the comments in another question , you seem to be adding rowstyles on every row, try adding your rows without adding styles or add one style first then add all the rows,.
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;
Upvotes: 9
Reputation: 236328
Thus you didn't post your code, I can't say what you are doing wrong. But this is the way you should add controls to your table layout panel:
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);
Of course you should have tableLayoutPanel.AutoScroll = true
BTW to avoid annoying horizontal scroll bar, you should add right padding to your table layout panel:
tableLayoutPanel.Padding =
new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
UPDATE AutoSize
should be disabled for tableLayoutPanel. Otherwise scroll will not appear - table layout panel will grow instead.
Upvotes: 5