如何禁用winforms表格面板的水平滚动条

发布于 2024-08-19 21:07:56 字数 109 浏览 2 评论 0原文

您好,我有一个表格布局面板,我正在动态地将控件绑定到它。当项目数超过面板高度时,明显出现垂直滚动条没有问题。

但同时,即使项目宽度小于面板宽度,也会出现水平滚动条。我怎样才能防止这种情况?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

冰雪之触 2024-08-26 21:07:56
int vertScrollWidth = SystemInformation.VerticalScrollBarWidth;

tableLayoutPanel1.Padding = new Padding(0, 0, vertScrollWidth, 0);
int vertScrollWidth = SystemInformation.VerticalScrollBarWidth;

tableLayoutPanel1.Padding = new Padding(0, 0, vertScrollWidth, 0);
被翻牌 2024-08-26 21:07:56

我遇到了这个问题,停靠的 TableLayoutPanel 包含停靠的 GroupBox 和设置为 100% 宽度的单个列。我不想为它们设置手动大小 - 我希望它们随着表单一起调整大小。

奇怪的是,将 TableLayoutPanel 的 Right Padding 设置为 1(不是滚动条的宽度 - 正如您所期望的那样,留下了滚动条大小的间隙)完全解决了这个问题。这是在 C# 2010 Express、.NET 4、Windows 8 中。不知道这个组合是否适用于其他变体。

将padding设置为0似乎在IDE中可以解决问题,但实际运行时问题依然存在。

对我来说,这就像 TableLayoutPanel 中的某种错误......或者可能只是我拥有的控件和属性的特定组合(这是一个非常复杂的布局)。

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

甜点 2024-08-26 21:07:56

今天在这个问题上掉了几根头发,但我解决了它,这就是我最终得到的结果:

  1. 创建一个继承自 TableLayoutPanel 的新类(我们称之为 MyTableLayoutPanel),并重写 MaximumSize 属性,如下所示:

    公共覆盖大小MaximumSize
    {
        得到
        {
            if(父级!= null)
                返回新的Size(Parent.Width, 0);
            别的
                返回基数.MaximumSize;
        }
        放
        {
            基数.MaximumSize = 值;
        }
    }
    

    您当然可以通过添加另一个属性来使其更通用,该属性决定是否应该返回更改后的 MaximumSize ,但希望这对于阅读本文的任何人来说都是显而易见的。

  2. 将您拥有的 TableLayoutPanel 更改为新的 MyTableLayoutPanel 类型。

  3. 将其添加到常规面板中。在此面板上启用 AutoScroll,而不是在 MyTableLayoutPanel 上(如果尚未禁用,请将其禁用)。

  4. 将 MyTableLayoutPanel AutoSize 属性设置为 true,并将其 Anchor 属性设置为 Left、Right 和 Top。

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.

筑梦 2024-08-26 21:07:56

我知道这个问题已经存在很久了。但可能其他人可能会从对我有用的解决方案中受益。

诀窍是,如果自动滚动属性为 true,则禁用水平滚动条不会执行任何操作。因此,只需禁用自动滚动,禁用水平滚动条,然后再次将自动滚动切换为 true 即可。这对我有用。

只需将以下代码添加到某个位置,例如构造函数或 Form Load 事件。

tableLayoutPanel1.AutoScroll = false;
tableLayoutPanel1.HorizontalScroll.Enabled = false;
tableLayoutPanel1.AutoScroll = true;

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;
凤舞天涯 2024-08-26 21:07:56

问题是您的项目恰好是布局面板的宽度,因此当垂直滚动出现时,它会稍微切入您的控件,从而强制水平滚动?如果是这样,您可以缩小控件的宽度以考虑滚动条的可能性,或者您可以尝试在滚动条出现时调整它们。

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.

放我走吧 2024-08-26 21:07:56

这在 .NET 3.5 中完美运行,而其他解决方案并没有给我完全我想要的东西:

  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;
    }
  }

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;
    }
  }
甲如呢乙后呢 2024-08-26 21:07:56

我遇到过这个问题。

很多人在 Datagrid 中也遇到同样的问题。但是,这个问题没有确切的解决方案,您必须根据用途手动决定面板的尺寸。

tableLayoutPanel1.HorizontalScroll.Enabled = false;

这将禁用水平滚动条,但您必须手动调整表格布局面板的尺寸。

另一种方法可以是在运行时计算 tablelayoutpanel 的可能宽度,如果它大于您设置的值,则可以启用它。

tableLayoutPanel1.HorizontalScroll.Enabled = true;

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;
尴尬癌患者 2024-08-26 21:07:56

我通过使用一个简单的面板解决了这个问题,我将表格布局面板停靠在其中。
然后我没有让 TLP 有滚动条,而是让面板有滚动条。这对我来说效果很好。

我假设具有不同列和行的 TLP 在计算每个列和行的宽度时存在问题,因此即使在不需要时也会显示垂直滚动条。

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.

胡大本事 2024-08-26 21:07:56

我通过使用reflect找到了关于这个问题的完美解决方案。
您可以尝试以下代码:

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;
}

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;
}
柠檬色的秋千 2024-08-26 21:07:56

这些解决方案并非在所有情况下都有效。我最终需要它们的组合:

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);
}

重要:这可以在表单加载时调用一次,但也必须在布局调整大小事件中调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文