Search code examples
c#.netwinformsscroll

Add vertical scroll bar to panel


I am trying to make a Panel scrollable, but only vertically (so AutoScroll won't work because the child controls go past the left edge and must).

So how is this done?


Solution

  • Assuming you're using winforms, default panel components does not offer you a way to disable the horizontal scrolling components. A workaround of this is to disable the auto scrolling and add a scrollbar yourself:

    ScrollBar vScrollBar1 = new VScrollBar();
    vScrollBar1.Dock = DockStyle.Right;
    vScrollBar1.Scroll += (sender, e) => { panel1.VerticalScroll.Value = vScrollBar1.Value; };
    panel1.Controls.Add(vScrollBar1);
    

    Detailed discussion here.