2

I want to be able to scroll inside a TableLayoutPanel if the content gets higher than the panel itself.
I have tried:

  • Set AutoScroll to true (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 to false and VerticalScroll.Visible to true (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();
    }
}
Community
  • 1
  • 1
Breeze
  • 2,010
  • 2
  • 32
  • 43

3 Answers3

3

The code you posted works for me, just set autoscroll to true and then tlp.HorizontalScroll.Visible = false;

EDIT: Just noticed the horizontal bar anyway, my resolution hidden it. Anyway, set the autoscroll to false, then add
tlp.AutoScroll = true; after tlp.ResumeLayout(); in Add10Lines() method.

Vegz
  • 106
  • 5
0

Why dont you try to set the Height of the HorizontalScroll to something bigger than the container.

what I want to say is that if the height of TableLayoutPanel is 300 make the height of the Scroll 350.

Try adding the height to the Scroll that should be greater than TableLayoutPanel height

Apoorv
  • 2,023
  • 1
  • 19
  • 42
0

You are using the below line in your code, and due to it you are not getting a vertical scrollbar:

tlp.Dock = DockStyle.Fill;

Instead of using that, try the below line, which may work for you:

tlp.Dock = DockStyle.Top;
Andrew Myers
  • 2,754
  • 5
  • 32
  • 40
Ajay Sharma
  • 31
  • 1
  • 6