Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
469 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...