简体   繁体   中英

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 Property AutoScroll = 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);

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM