简体   繁体   中英

Why i have vertical scrollbar in tablelayoutpanel

I'm trying to create a buttons dynamically and add them to table layout panel the problems is that no matter what i do i keep having a vertical scrollbar even if i have only one row of buttons. Code :

 private void button2_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 50; i++)
        {
            Button button = new Button();
         //   button.Location = new Point(20, 30 * i + 10);
        button.Click += new EventHandler(ButtonClick);
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
        tableLayoutPanel1.ColumnCount += 1;
        tableLayoutPanel1.Controls.Add(button);
        }
    }

Result : 在此处输入图片说明

I want to get rid of it the horizontal one is ok

Thanks in advance

Try adding the height of the horizontal scrollbar to the padding of the control:

private void button2_Click(object sender, EventArgs e)
{
  tableLayoutPanel1.Padding = new Padding(0, 0, 0, SystemInformation.HorizontalScrollBarHeight);

  for (int i = 0; i < 50; i++)
  {
    Button button = new Button();
    button.Click += new EventHandler(ButtonClick);
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 20F));
    tableLayoutPanel1.ColumnCount += 1;
    tableLayoutPanel1.Controls.Add(button);
  }
}
tableLayoutPanel1.VerticalScroll.Enabled = false;

应该摆脱你的问题

I work around the problem by padding my TableLayoutPanel control to account for the VeritcalScrollBarWidth.

This is VB.net but you get the point:

    'Prevent Constant Horizontal Scrollbar by padding for the VerticalBar
    MyTableLayoutPanel.Padding = New Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM