I want to be able to scroll inside a TableLayoutPanel
if the content gets higher than the panel itself.
I have tried:
- Set
AutoScroll
totrue
(adds unnecessary horizontal scrollbar) - Additionally add Padding on the right to prevent the horizontal scrollbar from appearing (leaves a nasty gap between content and scrollbar)
- set
AutoScroll
tofalse
andVerticalScroll.Visible
totrue
(the scrollbar ignores when the content grows and doesn't allow the user to scroll)
How can I get a pleasant vertical scrollbar without nasty side effects?
some runnable code to play with:
public class FormTLPTest : Form
{
TableLayoutPanel tlp;
public FormTLPTest()
{
Height = 800;
Width = 800;
tlp = new TableLayoutPanel();
tlp.Dock = DockStyle.Fill;
tlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
//tlp.AutoScroll = true; //adds horizontal ScrollBar
//tlp.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0); //prevents horizontal ScrollBar but adds nasty gap
tlp.VerticalScroll.Visible = true;
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0F));
Controls.Add(tlp);
tlp.Click += Tlp_Click;
Add10Lines();
}
private void Tlp_Click(object sender, System.EventArgs e)
{
Add10Lines();
}
private void Add10Lines()
{
tlp.SuspendLayout();
for (int i = 0; i < 10; i++)
{
Label lb2 = new Label();
lb2.Margin = new Padding();
lb2.Dock = DockStyle.Fill;
lb2.BackColor = Color.White;
lb2.Text = "Some longer Text - it contains information. Don't know what I should write to fill the space";
lb2.Click += Tlp_Click;
tlp.Controls.Add(lb2, 0, i);
}
tlp.ResumeLayout();
}
}