How to disable horizontal scrollbar for table panel in winforms
Asked Answered
U

10

18

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?

Utilize answered 4/2, 2010 at 5:15 Comment(2)
I realize this is 4 years old, however, I just had the same (or very similar problem today, and the fix was as simple as AutoScroll = false Hope this helps someone!Ferial
@cullub AutoScroll = false will also disable Vertical scrollbar, but the question is how to disable only Horizonatal scrollbarCiro
H
43
int vertScrollWidth = SystemInformation.VerticalScrollBarWidth;

tableLayoutPanel1.Padding = new Padding(0, 0, vertScrollWidth, 0);
Hardej answered 2/7, 2011 at 7:25 Comment(3)
Funny solution. Do you by any chance know why this sorcery works?Quiche
Tested this on .NET 4.6.2. It does remove h-scroll bar when the height is not enough, but the problem is it makes a wide margin on the right which does not disappear when the v-scroll bar appears. I want the TableLayoutPanel to fill the window width.Timeserver
where we put this code? In constructor of our form class? or where?Hallie
R
9

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).

Regolith answered 26/2, 2014 at 13:9 Comment(6)
confirmed for .NET 3.5, Windows 7Timmytimocracy
confirmed for .NET 4.6.2, Windows 10. Visual Studio 2017. Needed to set Padding right 9px though. 1 px only seemed to solve the problem in the editor.Khoury
confirmed for .NET 3.5, Windows 7, SharpDevelop 4.4. However I had to reach 6 px to make it workHaff
Thanks, padding 1 worked for .NET 4.5.1, windows 10, vs 2017. InterestingJoejoeann
Gotta add second comment: Today I had similar situation where the padding of 1px didin't work. But 2px did. So try different values.Joejoeann
Confirmed for .NET 4.7.2, Visual Studio 2019, Windows 10. Needed to set Padding.Right to 2px.Canberra
L
7

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.

Locomotor answered 24/8, 2011 at 12:5 Comment(1)
This is the only solution that really worked for my layout, thank you!Estrada
P
3

I know it has been long this question is here. But probably someone else may benefit from the solution worked for me.

The trick is that disabling the horizontal scroll bar does nothing if auto scroll property is true. So just disable auto scroll, disable horizontal scrollbar and then switch the auto scroll to true again. This works for me.

Just add the following code to some place such as the constructor or Form Load event.

tableLayoutPanel1.AutoScroll = false;
tableLayoutPanel1.HorizontalScroll.Enabled = false;
tableLayoutPanel1.AutoScroll = true;
Perceive answered 23/12, 2020 at 22:51 Comment(1)
There are solution and there are trick. Tricks are sign of experience, which should be admired, This solved my problem .Numb
S
1

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.

Scoria answered 4/2, 2010 at 5:28 Comment(0)
S
1

I have experienced this problem.

A lot of people also get same problem in Datagrid. But, there is no exact solution for this question you will have to manually decide the the dimension of the panel according to use.

tableLayoutPanel1.HorizontalScroll.Enabled = false;

this will disable the horizontal scroll bar but you will have to manually adjust the dimension of the table layout panel.

Another way could be calculating the possible width of tablelayoutpanel during run time and if it greater than the value you have set then you can enable it.

tableLayoutPanel1.HorizontalScroll.Enabled = true;
Shipman answered 1/12, 2010 at 8:22 Comment(1)
Does not work. The horizontal scrollbar appears just as before. The alternate Visible property doesn't work either.Mccants
J
1

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;
    }
  }
Jellyfish answered 11/9, 2014 at 14:54 Comment(0)
C
1

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.

Carson answered 1/10, 2015 at 13:36 Comment(0)
D
1

I found a perfect solution about this problem by using reflect. You can try following codes:

static MethodInfo funcSetVisibleScrollbars;
static EventHandler ehResized;

public static void DisableHorizontalScrollBar(this ScrollableControl ctrl)
{
     //cache the method info
     if(funcSetVisibleScrollbars == null)
     {
           funcSetVisibleScrollbars = typeof(ScrollableControl).GetMethod("SetVisibleScrollbars",
                BindingFlags.Instance | BindingFlags.NonPublic);
     }

     //init the resize event handler
     if(ehResized == null)
     {
           ehResized = (s, e) =>
           {
                funcSetVisibleScrollbars.Invoke(s, new object[] { false, (s as ScrollableControl).VerticalScroll.Visible });
           };
     }

     ctrl.Resize -= ehResized;
     ctrl.Resize += ehResized;
}
Dever answered 12/11, 2019 at 5:51 Comment(0)
C
0

None of these solution worked in every case; I ended up needing a combination of them:

public void hideHorizontalScrollBar(ref TableLayoutPanel pan)
{
    if (!pan.HorizontalScroll.Visible)
        return;
    pan.Padding = new Padding(0, 0, 0, 0);
    while (!!pan.HorizontalScroll.Visible || pan.Padding.Right >= SystemInformation.VerticalScrollBarWidth * 2)
    pan.Padding = new Padding(0, 0, pan.Padding.Right + 1, 0);
}

Important: this can be called once on form load, but must also be called in the layout resize event.

Conveyancer answered 9/11, 2020 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.