TableLayoutPanel displays vertical scroll

孤人 提交于 2019-11-29 01:21:16

问题


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:

Incorrect visibility:

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


回答1:


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.



回答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".




回答3:


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.




回答4:


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;
}


来源:https://stackoverflow.com/questions/15620454/tablelayoutpanel-displays-vertical-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!