How to disable horizontal scrollbar for table pane

2019-01-18 20:05发布

Hi I've a tablelayoutpanel and I'm binding controls to it dynamically. When the item count exceeds the height of panel obviously vertical scroll bar appearing there is no problem.

But the same time horizontal scroll bar is also appearing even the items width is less than the width of panel. How can i prevent this?

7条回答
相关推荐>>
2楼-- · 2019-01-18 20:28

This worked perfectly in .NET 3.5 where the other solutions did not give me exactly what I wanted:

  if (this.TableLayoutPanel1.HorizontalScroll.Visible)
  {
    int newWid = this.TableLayoutPanel1.Width -
                  (2 * SystemInformation.VerticalScrollBarWidth);
    //this.TableLayoutPanel1.Padding = new Padding(0, 0, newWid, 0);
    foreach (Control ctl in this.TableLayoutPanel1.Controls)
    {
      ctl.Width = newWid;
    }
  }
查看更多
混吃等死
3楼-- · 2019-01-18 20:33

Lost a few hairs on this today, but I solved it and this is what I ended up with:

  1. Create a new class that inherits from TableLayoutPanel (lets call it MyTableLayoutPanel), and override the MaximumSize property like this:

    public override Size MaximumSize
    {
        get
        {
            if (Parent != null)
                return new Size(Parent.Width, 0);
            else
                return base.MaximumSize;
        }
        set
        {
            base.MaximumSize = value;
        }
    }
    

    You could of course make it more general purpose by adding another property that decides whether or not you should return the altered MaximumSize or not, but hopefully that much is obvious to anyone that's reading this.

  2. Change the TableLayoutPanel that you've got to the new MyTableLayoutPanel type.

  3. Add it to a regular Panel. Enable AutoScroll on this Panel instead of the MyTableLayoutPanel (disable it there if you haven't already).

  4. Set the MyTableLayoutPanel AutoSize property to true and its Anchor property to Left, Right and Top.

查看更多
ら.Afraid
4楼-- · 2019-01-18 20:35

I solved this by using a simple Panel into which i docked the tablelayoutpanel. Then I made not the TLP have the scrollbars, but the Panel. This works fine for me.

I assume the TLP with the different columns and rows has issues to calculate the width for each and thus shows a vertical scrollbar even when not necessary.

查看更多
\"骚年 ilove
5楼-- · 2019-01-18 20:43

Is the issue that your items are exactly the width of the the layout panel, so that when the verticle scroll appears it cuts into your controls a bit, forcing the horizontal scroll? If so, you can either make your controls smaller width-wise to account for the possibility of the scrollbar, or you can try to adjust them when the scroll bar appears.

查看更多
再贱就再见
6楼-- · 2019-01-18 20:46

I had this problem with a docked TableLayoutPanel containing docked GroupBoxes and a single Column set to 100% width. I didn't want to set a manual size for these - I wanted them to resize along with the form.

Strangely, setting the Right Padding of the TableLayoutPanel to 1 (not the width of the scrollbar - that left a scrollbar-sized gap, as you would expect) solved the issue completely. This is in C# 2010 Express, .NET 4, Windows 8. No idea if this kludge works on other variations.

Setting the padding to 0 seemed to solve the problem in the IDE, but the problem still existed when actually running.

Smells like some sort of bug in the TableLayoutPanel to me...or maybe it's just the particular combination of controls and properties I have (it's a pretty complicated layout).

查看更多
祖国的老花朵
7楼-- · 2019-01-18 20:52
int vertScrollWidth = SystemInformation.VerticalScrollBarWidth;

tableLayoutPanel1.Padding = new Padding(0, 0, vertScrollWidth, 0);
查看更多
登录 后发表回答