How do I disable the horizontal scrollbar in a Panel

前端 未结 11 1884
别那么骄傲
别那么骄傲 2020-12-01 12:13

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

HorizontalScroll.Enabled = false;

But tha

相关标签:
11条回答
  • 2020-12-01 12:34
    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.

    0 讨论(0)
  • 2020-12-01 12:35

    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
    
    0 讨论(0)
  • 2020-12-01 12:37

    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.

    0 讨论(0)
  • 2020-12-01 12:38

    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.

    0 讨论(0)
  • 2020-12-01 12:41

    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();
    
    0 讨论(0)
  • 2020-12-01 12:44

    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

    0 讨论(0)
提交回复
热议问题