How to make the vertical scrollbar always be visible from AutoScroll in WinForms?

Using VS2010 and .NET 4.0 with C # and WinForms:

I always want the vertical scroll bar to appear for my panel as a disabled scroll bar (when it is not needed and on when it can be used.

So, it looks like a hybrid AutoScroll. I tried using VScrollBars, but I can’t figure out where to place them to do this job.

Essentially, I have a user control that acts like a “Document” of controls, its size changes, so when using automatic scrolling, it works great. A scrollbar appears when the user control is not suitable, and the user can move it updown.

It looks like a web browser. However, redrawing controls take a lot of time (it is formed with many fields and buttons, etc. Inside groups in a grid inside a panel: P

Somehow, when autoscroll allows a vertical scrollbar, it takes some time to redraw the window. I would like ALWAYS to show a vertical scroll bar as described above (with the ability to turn it on / off).

If anyone has any help, I read a lot of posts on the topic of auto-scrolling, but no one asked what I ask, and I can not come up with a solution.

+5
source share
4 answers

C # Version of a competent answer to a question

using System.Runtime.InteropServices; 

public class MyUserControl : UserControl
{
    [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
    }

    public MyUserControl()
    {
        InitializeComponent();
        ShowScrollBar(this.Handle, (int) ScrollBarDirection.SB_VERT, true);
    }
}
+4
source

, , :

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

Private Const SB_VERT As Integer = 1


Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ShowScrollBar(Panel1.Handle, SB_VERT, True)
End Sub

, , , . , , , , .

, , SuspendLayout ResumeLayout .

+3

. , , - . , , 1920x1080. . , , AutoScroll true. ( ). , , , ... . , . , , .

The actual size of the panel is the one you limit when displaying, but the virtual size is dictated by an invisible control.

0
source

What worked for me was overriding the call CreateParamsand turning on the style WS_VSCROLL.

public class VerticalFlowPanel : FlowLayoutPanel
{
    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            cp.Style |= 0x00200000; // WS_VSCROLL
            return cp;
        }
    }
}

Now the logic AutoScrollwill adjust the scroll borders without hiding the scroll bar.

0
source

All Articles