如何完全删除FlowLayoutPanel中的HorizontalScrollBar

本文关键字:中的 HorizontalScrollBar FlowLayoutPanel 删除 何完全 | 更新日期: 2023-09-27 17:57:44

我有带多个控件的FlowLayoutPanel。通常水平滚动条是不可见的,它是可以的。但当我调整大小时,水平滚动条会出现并闪烁。调整大小后,它将不再可见。

我尝试过的:

1)

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

这并不完全奏效。

2)

flowlayoutPanel.AutoScroll = false;
flowlayoutPanel.HorizontalScroll.Enabled = false;
flowlayoutPanel.HorizontalScroll.Visible = false;
flowlayoutPanel.AutoScroll = true;

在此之后,水平滚动条被禁用,但仍然可见。

3)

flowlayoutPanel.AutoScroll = true;
flowlayoutPanel.WrapContents = false;

这并不完全奏效。

4)

[DllImport("user32.dll")]
static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);
protected override void OnShown(EventArgs e) {
    ShowScrollBar(this.flowlayoutPanel.Handle, SB_HORZ, false);
    base.OnShown(e);
}

这并不完全奏效。

5)

flowlayoutPanel.SuspendLayout();
//resize controls inside flowlayoutPanel
flowlayoutPanel.ResumeLayout();

更好的是,水平滚动条闪烁更少,但仍然闪烁

6)

//ResizeBegin event
flowlayoutPanel.AutoScroll = false;
//ResizeEnd event
flowlayoutPanel.AutoScroll = true;

这并不完全奏效。

如何完全删除FlowLayoutPanel中的HorizontalScrollBar

我知道这是一篇旧帖子,但我发现自己是这个问题的解决方案。(我为VerticalScrollbar做了这件事,因为这是我在TopDown FlowDirection FlowLayoutPanel上的问题),但也可以为HorizontalScrollbar进行代码转换。

// pContent is my FlowLayoutPanel, it has FlowDirection set to TopDown
// and WrapContent = true;
pContent.SuspendLayout();
// Populate the FlowLayoutPanel with controls
pContent.ResumeLayout();
// I want to show only HorizontalScrollbar
if (pContent.VerticalScroll.Visible)
    {
        pContent.AutoScroll = false;
        pContent.VerticalScroll.Visible = false;
        pContent.Height -= SystemInformation.HorizontalScrollBarHeight;
        pContent.AutoScroll = true;
        pContent.Height += SystemInformation.HorizontalScrollBarHeight;    
    }

因此,对于隐藏水平滚动条:

// pContent has FlowDirection set to LeftRight and WrapContent = true;
pContent.SuspendLayout();
// Populate the FlowLayoutPanel with controls
pContent.ResumeLayout();
// I want to show only VerticalScrollbar
if (pContent.HorizontalScroll.Visible)
    {
        pContent.AutoScroll = false;
        pContent.HorizontalScroll.Visible = false;
        pContent.Width -= SystemInformation.VerticalScrollBarWidth;
        pContent.AutoScroll = true;
        pContent.Width += SystemInformation.VerticalScrollBarWidth;    
    }