繁体   English   中英

如何在C#System.Windows.Forms.Panel中禁用水平滚动条

[英]How to disable a Horizontal Scrollbar in C# System.Windows.Forms.Panel

我尝试添加此先前线程中的代码,但对我而言不起作用。

如何禁用面板中的水平滚动条

我只是想删除它,因为无论是否需要在屏幕上显示它,它都会显示出来。

除非通过Panel.AutoScroll = falsepanel1.HorizontalScroll.Visible = true进行静态设置,否则面板不会显示条形图。 我建议您确认没有控件超出面板范围,而不是强制执行状态。

将以下内容插入表单的某些部分。 这将验证您没有控件超出面板的侧面。 将panel1更改为有问题的面板的名称。

        foreach (Control comp in panel1.Controls)
        {
            if (comp.Right >= panel1.Width || comp.Bottom >= panel1.Height)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

如果仍然找不到问题, Panel.AutoScroll = false应该执行Panel.AutoScroll = falsepanel1.HorizontalScroll.Visible = false

我发现此解决方案是首选。

public class MongoDataPanel : Panel
{

    [DllImport("user32.dll")]
    static public extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);
    private const int SB_HORZ = 0;
    private const int SB_VERT = 1;


    protected override void OnResize(EventArgs eventargs)
    {
        base.OnResize(eventargs);
        if (this.Parent != null)
        {
            if (this.Parent.Width > this.PreferredSize.Width - 10)
            {
                try
                {
                    ShowScrollBar(this.Handle, SB_HORZ, false);
                }
                catch (Exception e) { }
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM