TableLayoutPanel displays vertical scroll
Asked Answered
P

6

20

I have TableLayoutPanel for dynamic creation of controls with AutoScroll = true. It's work fine when I add new controls. But when I remove and all controls are visible, vertical scroll is visible. Some screenshots here:

Expected/correct scroll visibility:

enter image description here

Incorrect visibility:

enter image description here

Any ideas?

Update: Here is some code

tableLayoutPanel1.SuspendLayout();
tableLayoutPanel1.RowCount = 0;
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
foreach (var item in objects)
{
     tableLayoutPanel1.RowCount++;
     tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
     tableLayoutPanel1.Controls.Add(CreateNewItem(item));
 }

 tableLayoutPanel1.RowCount++;
 tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
 tableLayoutPanel1.Controls.Add(CreateAddButton());

 tableLayoutPanel1.ResumeLayout();

and code for deleting

tableLayoutPanel1.SuspendLayout();
tableLayoutPanel1.Controls.Remove(item);
tableLayoutPanel1.RowStyles.RemoveAt(0);
tableLayoutPanel1.RowCount--;
tableLayoutPanel1.ResumeLayout();

AutoSize is true, AutoSizeMode is GrowAndShrink

Pendley answered 25/3, 2013 at 16:54 Comment(3)
When you remove the controls, are you removing the rows, too?Execration
@Execration yes. Last row has AutoSize style, but it's look like Percent.Pendley
.Net Framework 4.8 this bug still existsZeuxis
R
66

The problem concerns TableLayoutPanel scrolling.
You have to use a Panel for scrolling instead of TableLayoutPanel.
Here is an example to solve this problem (for vertical scrolling) :

  • Set your TableLayoutPanel properties as follow :
    • Dock = DockStyle.Top
    • AutoSize = true
    • AutoSizeMode = AutoSizeMode.GrowAndShrink
    • AutoScroll = false.
  • Put your TableLayoutPanel into a Panel with properties :
    • Dock = DockStyle.Fill
    • AutoScroll = true
    • AutoSize = false.
Runnymede answered 1/9, 2014 at 7:50 Comment(4)
7 years later and this is still a working solution. But why is setting DockStyle.Top required (as opposed to Fill) on the TableLayoutPanel? Without it, empirically, it doesn't work - but I don't understand the logic.Franzoni
If you set DockStyle.Fill, the TableLayoutPanel will have the same dimension than its parent, so the Panel will never display the scrolling bar.Runnymede
THIS is why I SO. What a golden answer in a time of need.Gerhardine
For horizontal scrolling, I set Dock = DockStyle.Left for my TableLayoutPanel and solved the problem.Hypothesis
S
2

when you remove the dynamic controls, you need to remove the extra rows that was inserted during the addition and re-size the table layout panel height to smaller than scroll container height.

During the addition the table layout panel height would have increased, which handled by the scroll container; but when you remove the controls, the table layout panel height doesn't reduce it's height to fit the scroll container.

One way to do this is to give fixed height to the rows and set the table layout panel seize set to "Auto".

Sadick answered 25/3, 2013 at 17:29 Comment(1)
Each row has AutoSize style and tableLayoutPanel.AutoSize is truePendley
O
1

One of the easiest and funniest solution is to just disable and enable tableLayoutPanel1.AutoScroll

In your Deleting procedure code add at the end these codes :

    tableLayoutPanel1.AutoScroll = False
    tableLayoutPanel1.AutoScroll = True
Osmium answered 17/9, 2020 at 18:35 Comment(0)
P
0

I inserted tableLayoutPanel to XtraScrollableControl(Devexpress control). tableLayoutPanel.Dock set to Top and XtraScrollableControl.Dock to Fill. This solution did not solves this problem, but I got behavior that I need.

Pendley answered 26/3, 2013 at 8:22 Comment(0)
F
0

I counted the number of rows in my TableLayoutPanel to see how many would fit. Below the amount that fit I set AutoScroll = false for the add and delete methods. The scroll will appear for large sets and disappear on small sets.

if (tableLayoutPanel.RowCount < 15)
{
    panel1.AutoScroll = false;
}
else
{
     panel1.AutoScroll = true;
}
Fiesta answered 1/9, 2017 at 16:8 Comment(0)
O
-1

I had a TableLayoutPanel on a UserControl, docked in Fill mode, with all rows on the TableLayoutPanel set to AutoSize. This UserControl would then dynamically get put on a panel, again in Fill mode, to show it to the user when needed. I put the UserControl on AutoScroll, but that alone did not solve it.

In the end, I solved it by going over all controls in the TableLayoutPanel, storing the extremities, and baking that into a Size to put in my UserControl's AutoScrollMinSize:

private void AdjustPanelSize(ScrollableControl panel, TableLayoutPanel tableLayoutPanel)
{
    int maxX = 0;
    int maxY = 0;
    foreach (Control c in tableLayoutPanel.Controls)
    {
        maxX = Math.Max(maxX, c.Location.X + c.Width);
        maxY = Math.Max(maxY, c.Location.Y + c.Height);
    }
    panel.AutoScrollMinSize = new Size(maxX, maxY);
}

This worked, and it also has the advantage that it can be called if there would ever be controls dynamically added or removed from the TableLayoutPanel.

Orazio answered 22/9, 2022 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.