Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoscroll property of TableLayoutPanel is not working

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);
like image 469
Osama Jameel Ahmad Avatar asked Sep 20 '25 15:09

Osama Jameel Ahmad


2 Answers

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;
like image 60
xeon111 Avatar answered Sep 22 '25 07:09

xeon111


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.

like image 35
Sergey Berezovskiy Avatar answered Sep 22 '25 05:09

Sergey Berezovskiy