34

I have a panel (Windows Forms) and I want to disable a panels horizontal scrollbar. I tried this:

HorizontalScroll.Enabled = false;

But that wouldn't work.

How can I do this?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Eli Braginskiy
  • 2,867
  • 5
  • 31
  • 46

11 Answers11

83

Try to implement this way, it will work 100%

panel.HorizontalScroll.Maximum = 0;
panel.AutoScroll = false;
panel.VerticalScroll.Visible = false;
panel.AutoScroll = true;
Peter Gordon
  • 1,075
  • 1
  • 18
  • 38
Kbv Subrahmanyam
  • 962
  • 1
  • 7
  • 6
  • 9
    This should be the accepted answer. None of the other worked for me, yet this simple code actually fixed it! – Anonymous Pi Aug 04 '15 at 20:18
  • @AnonymousPi Tried a lot to get this Fixed. A day waste for me to actually fix it so simple. – Kbv Subrahmanyam Nov 30 '15 at 12:04
  • This is an awesome, and simple, answer to getting rid of the horizontal scroll bar. I included this solution (with credit to @Kbv Subrahmanyam !) in a more general solution to autoscroll problems that I was having here ... http://stackoverflow.com/a/35065017/2175233 – Brad Oestreicher Jan 28 '16 at 15:13
  • 9
    Something I kept getting hung up on that is very important! MAKE SURE the "AutoScroll" property is set to `FALSE` when in design mode or this will not work. – Arvo Bowen Apr 30 '16 at 01:29
  • 4
    What the hell is this witchcraft... That order of commands makes no logical sense. But it works... – Hill Sep 06 '18 at 15:55
  • 1
    The line "panel.AutoScroll = false;" should be the first line in the block if you have AutoScroll set to true in design mode. It then worked perfectly for me. – Brian Cryer Feb 07 '19 at 15:45
  • Using this trick I got rid of my vertical scroll bar. Finally. But it still scrolls up and down several pixels when you turn the mouse wheel. It's been annoying. – Kirk Hawley Oct 01 '19 at 22:22
  • 1
    Setting `VerticalScroll.Visible` or `HorizontalScroll.Visible` is irrelevant. Just set `AutoScroll = false`, set `VerticalScroll.Maximum` and/or `VerticalScroll.Maximum` to `0`, reset `AutoScroll = true` in the end. -- You won't be able to control the scroll range. – Jimi Mar 13 '22 at 21:28
  • I mean, you can set the AutoScrollPosition, but the mouse wheel scroll uses default values, which makes the scroll not so *smooth*. – Jimi Mar 13 '22 at 22:02
23

If you feel like desecrating your code you could try this very "hackish" solution:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);

private enum ScrollBarDirection
{
    SB_HORZ = 0,
    SB_VERT = 1,
    SB_CTL = 2,
    SB_BOTH = 3
}

protected override void WndProc(ref System.Windows.Forms.Message m)
{
    ShowScrollBar(panel1.Handle, (int)ScrollBarDirection.SB_BOTH, false);
    base.WndProc(ref m);
}

I'm currently using the code above to prevent a 3rd party UserControl from showing its scrollbars. They weren't exposing any proper ways of hiding them.

SuperOli
  • 1,784
  • 1
  • 11
  • 23
  • well i dont think it used for me,i made a little test and used it,and one panel just dissapeared and the other still had the scroll bars. thanks anayway :) – Eli Braginskiy Mar 31 '11 at 11:24
  • 5
    Works great! Though the `WndProc` part is definitely not needed. Just call: `ShowScrollBar(panel1.Handle, (int)ScrollBarDirection.SB_HORZ, false);` From another method anytime you want to hide that scroll bar! – BrainSlugs83 Nov 24 '11 at 00:26
  • It's showing error on win 2008 server while closing the form! – Tamilmaran Dec 07 '12 at 09:40
14

I think that you're having this problem because the AutoScroll property of your panel is set to true. I made a test solution (.NET 3.5) and discovered the following:

If you try this:

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

the HorizontalScroll.Enabled and .Visible aren't changed to false (assuming the panel has controls within that cause autoscroll to show the horizontal scroll bar). It seems that you must disable AutoScroll to be able to change these properties around manually.

Dave M
  • 315
  • 2
  • 8
7

I was having this same type of issue with the horizontal scroll appearing when AutoScroll=true, it only showed up when the vertical scrollbar appeared. I finally figured out that I removed padding from the panel and by adding 20 back to the right padding it allowed the vertical scrollbar to appear and not show the horizontal one.

JeffR
  • 1,782
  • 1
  • 15
  • 19
  • 1
    I have a scrollable TableLayoutPanel docked to Fill its parent container, and just adding 20px right padding to the TableLayoutPanel worked (the horizontal scroll bar no longer appears). – Rachel Jan 11 '17 at 11:28
2

SuperOli´s answer was great!

I updated the code so it is possible to close the form without any error.
I hope it works for you as well.

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);

private enum ScrollBarDirection
{
    SB_HORZ = 0,
    SB_VERT = 1,
    SB_CTL = 2,
    SB_BOTH = 3
}

protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg == 0x85) // WM_NCPAINT
    {                
        ShowScrollBar(panel1.Handle, (int)ScrollBarDirection.SB_BOTH, false);
    }

    base.WndProc(ref m);
}
Guesting
  • 21
  • 1
1
Console.BufferHeight = 44;

Set the buffer height equals to the console's window height. In my program I give the static height but you can do like this as well:

Console.BufferHeiht = (Console.WindowHight - 1);

I think it should work.

1

Managed C++ code to hide HScroll Bar:

// Hide horizontal scroll bar
HWND h = static_cast<HWND> (this->Handle.ToPointer());
ShowScrollBar(h, SB_HORZ, false);
Charles
  • 19
  • 1
0

Another solution, which seemed to be the only one I could get working was the rather hacky:

        foreach (UserControl control in ListPanel.Controls)
        {
            if (ListPanel.VerticalScroll.Visible)
                control.Width = ListPanel.Width - 23;
            else
                control.Width = ListPanel.Width-5;
        }

Which I call in its OnResize event

chrispepper1989
  • 2,100
  • 2
  • 23
  • 48
0

I was looking for an answer to the question how to add a scrollbar to a box when needed and remove it when not needed. This is the solution i came up with for a textbox, with maximum allowed width and height, resized for the text displayed.

private void txtOutput_TextChanged(object sender, EventArgs e)
    {
        // Set the scrollbars to none. If the content fits within textbox maximum size no scrollbars are needed. 
        txtOutput.ScrollBars = ScrollBars.None;

        // Calculate the width and height the text will need to fit inside the box
        Size size = TextRenderer.MeasureText(txtOutput.Text, txtOutput.Font);

        // Size the box accordingly (adding a bit of extra margin cause i like it that way)
        txtOutput.Width = size.Width + 20;
        txtOutput.Height = size.Height + 30;


        // If the box would need to be larger than maximum size we need scrollbars

        // If height needed exceeds maximum add vertical scrollbar
        if (size.Height > txtOutput.MaximumSize.Height)
        {

            txtOutput.ScrollBars = ScrollBars.Vertical;

            // If it also exceeds maximum width add both scrollbars 
            // (You can't add vertical first and then horizontal if you wan't both. You would end up with just the horizontal) 
            if (size.Width > txtOutput.MaximumSize.Width)
            {
                txtOutput.ScrollBars = ScrollBars.Both;
            }
        }

        // If width needed exceeds maximum add horosontal scrollbar 
        else if (size.Width > txtOutput.MaximumSize.Width)
        {
            txtOutput.ScrollBars = ScrollBars.Horizontal;
        }
    }
0

I just encountered this same kind of glitch with a scrollable UserControl in a form when switching from scrolling mode to zoom-to-fit mode. For the latter mode, both scrollbars should be off. Most of the time this change of mode works perfectly, except when either or both scrollbars are at their maximum value, causing one or other of the scrollbars to remain visible but disabled.

Which of the scrollbars remains visible seems to be random, so there must be some kind of race condition in the Windows code.

Curiously, the ClientRectangle reported by Windows is the full rectangle of the UserControl without any scrollbars. This means Windows thinks that the scrollbars are not visible, yet it still randomly displays one or other disabled scrollbar.

I solved it by moving the AutoScrollPosition to (0, 0) before performing the switch to zoom-to-fit mode.

AutoScrollPosition = new Point(0, 0);  // Bug fix to prevent one or other of the scrollbars randomly remaining visible when switching to zoom-to-fit mode when the scrollbars are at their maximum values.
zoom_factor = CalcZoomFactorBestFit();
SetScrollSizes();
Invalidate();
tim
  • 482
  • 3
  • 12
0

Following the code proposed by @Guesting, I have written a custom class for the panel control for enabling the double buffer option and also with possibility of enabling / disabling each scrollbar.

   Public Class PanelDoubleBuffer
    Inherits Panel

    'MAIN LAYOUT design scheme
    Public Property ShowVerticalScrolBar As Boolean = False
    Public Property ShowHorizontalScrolBar As Boolean = False

    Public Sub New()
        SuspendLayout()

        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.UserPaint, True)

        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        SetStyle(ControlStyles.ResizeRedraw, True)
        Me.UpdateStyles()

        ResumeLayout()
    End Sub

    <DllImport("user32.dll")>
    Private Shared Function ShowScrollBar(ByVal hWnd As IntPtr, ByVal wBar As Integer, ByVal bShow As Boolean) As Boolean
    End Function

    Public Property SB_HORZ As Integer = ShowHorizontalScrolBar
    Public Property SB_VERT As Integer = ShowVerticalScrolBar
    Public Property SB_CTL As Integer = 2
    Public Property SB_BOTH As Integer = 3

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = &H85 Then
            ShowScrollBar(Me.Handle, CInt(SB_BOTH), False)
        End If

        MyBase.WndProc(m)
    End Sub
End Class
Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23