Seth Carnegie
Seth Carnegie

Reputation: 75150

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?

Upvotes: 57

Views: 219324

Answers (8)

SandstormNick
SandstormNick

Reputation: 2021

Adding on to the answer by Kamgman which does work.

Let's say we were adding a label as the child control to the panel:

  • First add the panel. Set AutoScroll to True and AutoSize to False.
  • Add a label into the panel. Set AutoSize to true. You could give it a MinimumSize for the Width if you like so that it at least keeps its "shape" horizontally.
  • Then set the Anchor for the label control to Top only. Remove the Left anchor on it. This ensures the label only scrolls vertically but not horizontally.

If you go this route you don't have to add the lines to hide the horizontal scroll bar.

It might also be better if you're using System.ComponentModel.ComponentResourceManager.ApplyResources to load it from the .resx file instead of the .Designer.cs . Because in my case whenever I make edits to this particular form I lose the changes in the Designer.cs file. But that will come down to how your project is set up

Upvotes: 0

Ebrahim Qaashah
Ebrahim Qaashah

Reputation: 11

3 steps:

1- just set AutoScroll property to true

2- in Form load()add the following:

   my Panel Vertical Scroll Maximum = 10000     

3- after my Panel controls Add(item) add the following: Invalidate();
Done!

Upvotes: 1

OldMember
OldMember

Reputation: 59

AutoScroll is really the solution! You just have to set AutoScrollMargin to 0, 1000 or something like this, then use it to scroll down and add buttons and items there!

Upvotes: 6

123iamking
123iamking

Reputation: 2703

Panel has an AutoScroll property. Just set that property to True and the panel will automatically add a scroll bar when needed.

Upvotes: 5

kamgman
kamgman

Reputation: 797

Try this instead for 'only' scrolling vertical.
(auto scroll needs to be false before it will accept changes)

mypanel.AutoScroll = false;
mypanel.HorizontalScroll.Enabled = false;
mypanel.HorizontalScroll.Visible = false;
mypanel.HorizontalScroll.Maximum = 0;
mypanel.AutoScroll = true;

Upvotes: 55

Teoman Soygul
Teoman Soygul

Reputation: 25742

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.

Upvotes: 36

glinatser
glinatser

Reputation: 31

Below is the code that implements custom vertical scrollbar. The important detail here is to know when scrollbar is needed by calculating how much space is consumed by the controls that you add to the panel.

panelUserInput.SuspendLayout();
panelUserInput.Controls.Clear();
panelUserInput.AutoScroll = false;
panelUserInput.VerticalScroll.Visible = false;

// here you'd be adding controls

int x = 20, y = 20, height = 0;
for (int inx = 0; inx < numControls; inx++ )
{
    // this example uses textbox control
    TextBox txt = new TextBox();
    txt.Location = new System.Drawing.Point(x, y);
    // add whatever details you need for this control
    // before adding it to the panel
    panelUserInput.Controls.Add(txt);
    height = y + txt.Height;
    y += 25;
}
if (height > panelUserInput.Height)
{
    VScrollBar bar = new VScrollBar();
    bar.Dock = DockStyle.Right;
    bar.Scroll += (sender, e) => { panelUserInput.VerticalScroll.Value =  bar.Value; };
    bar.Top = 0;
    bar.Left = panelUserInput.Width - bar.Width;
    bar.Height = panelUserInput.Height;
    bar.Visible = true;
    panelUserInput.Controls.Add(bar);
}
panelUserInput.ResumeLayout();

// then update the form
this.PerformLayout();

Upvotes: 3

Lukas
Lukas

Reputation: 2923

Add to your panel's style code something like this:

<asp:Panel ID="myPanel" runat="Server" CssClass="myPanelCSS" style="overflow-y:auto; overflow-x:hidden"></asp:Panel>

Upvotes: -2

Related Questions