如何在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 = 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) { }
            }
        }
    }
}

最新更新