Autoscroll property of TableLayoutPanel is not working

11,558

Solution 1

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;

Solution 2

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.

Share:
11,558
Osama Jameel Ahmad
Author by

Osama Jameel Ahmad

Software Engineer at GameView Studios, Lahore, Pakistan.

Updated on June 05, 2022

Comments

  • Osama Jameel Ahmad
    Osama Jameel Ahmad almost 2 years

    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);
    
  • Osama Jameel Ahmad
    Osama Jameel Ahmad over 11 years
    this is my code and it is still 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);
  • Sergey Berezovskiy
    Sergey Berezovskiy over 11 years
    this.tableLayoutPanel1.RowCount = 1; you are setting row count to one. That's the problem. Also set auto scroll to true in designer.
  • Osama Jameel Ahmad
    Osama Jameel Ahmad over 11 years
    I also set auto-scroll = true in Designer Properties and also removed the line this.tableLayoutPanel1.RowCount = 1; but still not working
  • Sergey Berezovskiy
    Sergey Berezovskiy over 11 years
    @OsamaJameelAhmad what about row count? Have you fixed that part? You should set appropriate row count.
  • Osama Jameel Ahmad
    Osama Jameel Ahmad over 11 years
    yes I replaced it with your code tableLayoutPanel.RowCount = tableLayoutPanel.RowStyles.Count; in my code
  • Sergey Berezovskiy
    Sergey Berezovskiy over 11 years
    @OsamaJameelAhmad use my code, just replace YourControl with CheckBox
  • Osama Jameel Ahmad
    Osama Jameel Ahmad over 11 years
    I also tried this still not working I cant only see the last record added the older records can't be seen now due to the small size of tablyelayoutpanel still no scroll bar appears
  • Sergey Berezovskiy
    Sergey Berezovskiy over 11 years
    @OsamaJameelAhmad looks like you have set AutoSize = true to your table layout panel. Auto size should be disabled.
  • Osama Jameel Ahmad
    Osama Jameel Ahmad over 11 years
    @lazeberezovsky AutoSize = false. Where I can send you screen shots of my code and ouput? Do you have facebook?
  • Osama Jameel Ahmad
    Osama Jameel Ahmad over 11 years
    @lazeberezovsky kindly tell me where I can send you the screen short of output?
  • Sergey Berezovskiy
    Sergey Berezovskiy over 11 years
    @OsamaJameelAhmad add you code and screenshot to the question
  • David
    David about 11 years
    Note: In Visual Studio 2010, after putting "SystemInformation.VerticalScrollBarWidth" as Padding value, when returning to the design page, it was changed to value of 16 both there and in the Designer.cs.
  • Breeze
    Breeze about 8 years
    I had to use different styles for every row, so not adding styles was not an option. Adding a row without style at the end solved the problem though