squiguy
squiguy

Reputation: 33380

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

I tried adding the code from this previous thread, but it did not work for me.

How do I disable the horizontal scrollbar in a Panel

I simply just want to remove it, because it shows up regardless of if it needs to be or not be on my screen.

Upvotes: 0

Views: 4281

Answers (2)

dexiang
dexiang

Reputation: 1413

I found this solution is prefered.

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

Upvotes: 0

Mitch
Mitch

Reputation: 22311

A panel will not show bars unless it is either statically set to do so via Panel.AutoScroll = false and panel1.HorizontalScroll.Visible = true. I would reccomend you verify that no controls extend beyond the panel, rather than forcing the status.

Insert the following into some part of your form. This will verify that you don't have a control that extends beyond the sides of the panel. Change panel1 to the name of the panel that has issues.

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

If you still cannot find the issue, Panel.AutoScroll = false and panel1.HorizontalScroll.Visible = false should do the job.

Upvotes: 1

Related Questions