如何在FlowLayoutPanel中禁用水平滚动条?

24

我有一个FlowLayoutPanel,上面有多个控件。我只想垂直方向滚动,但是当我设置AutoScroll = true时,我得到了水平和垂直滚动条。如何禁用水平滚动条并保留仅垂直滚动条的功能?

你可以在代码中设置HorizontalScroll.Maximum = 0;来禁用水平滚动条,并确保VerticalScroll.Visible = true;以保留垂直滚动条的功能。
3个回答

65
  • AutoScroll 设置为 true。
  • WrapContents 设置为 false。
  • 确保大小宽于控件的宽度加上垂直滚动条的宽度。

水平滚动条应该消失。如果没有,请提供更多信息。


2
谢谢!刚才我试了一下,发现如果我设置flowDirection=leftToRight,flowlayoutPanel.HorizontalScroll.Visible = false,wrapContents = true,它就可以工作了...所以有多种方法可以做到这一点吗?无论如何,还是谢谢你! :) - spspli
1
我注意到启用WrapContents(使用从左到右的流)会创建“换行符”,因此永远不需要任何水平滚动条。实际上,我不喜欢这样,我想将WrapContents设置为false,但我仍然希望FlowBreak属性起作用,手动控制换行符。然而,这并不起作用。如果WrapContents为false,则该控件的FlowBreak属性会被忽略,这是一个纯粹的错误。微软又来了。 - v.oddou
1
我正在使用WrapContents。为了达到我想要的结果,我将我的FlowLayoutPanel包含在一个Panel控件中,然后将Panel的AutoScroll设置为true,FlowLayoutPanel的自动滚动设置为false,这样就可以了! - Larry

5

将AutoScroll设置为true。 将WrapContents设置为false。 将Padding Right设置为10。

对于我来说,这很好用。


我本以为“不可能,它不会起作用”,但是是的,右填充起了作用:S - Cesar
这个技巧很有效。唯一的问题是,根据子控件的字体大小,您需要调整填充。 - Prem
不如禁用垂直滚动条,而不是需要水平滚动条怎么样?@user2559770? - gumuruh

-1

以下是我如何实现在FlowLayoutPanel上具有多个标签和自动换行(WrapContents = true),并且只有垂直滚动条。

  1. 我在表单上放置了一个flowLayoutPanel1
  2. 设置表单和flowLayoutPanel1的属性如下:

表单:

AutoScroll = True
FormBorderStyle = Sizable(default)

flowLayoutPanel1:

Anchor = Top, Left, Right
AutoSize = True
FlowDirection = TopDown
WrapContents = true

将此代码实现在表单类中进行测试。

int coorY = 0;
        public Form2()
        {
            InitializeComponent();
            for (int i = 0; i < 100; i++)
            {
                flowLayoutPanel1.Controls.Add(new Label 
                { 
                    Location = new Point(0, coorY + 20),
                    Font = new Font("Segoe UI", 10f),
                    Text = "I have a FlowLayoutPanel and there are multiple controls on it. I only want to scroll in vertical",
                    Width = flowLayoutPanel1.Width,
                    AutoSize = true
                });
                coorY += 20;
            }
        }

垂直滚动条的实现


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接