5

How do I set my panel so it will only scroll horizontal? I have tried:

this.VerticalScroll.Enabled = false;

But it doesn't work.

Klaasvaak
  • 5,634
  • 15
  • 45
  • 68
  • Is "this" the panel or the hosting form? How does the "not working" manifest itself? – kaj Feb 24 '12 at 09:51
  • this is the panel. I can still scroll both ways when setting VerticalScroll.Enabled to false. – Klaasvaak Feb 24 '12 at 09:58
  • 1
    similar question http://stackoverflow.com/questions/5489273/how-to-disable-the-horizontal-scrollbar-in-a-panel – CuriousMind Feb 24 '12 at 11:40
  • Thanks for the link, I fixed it now. Had to change it a bit though – Klaasvaak Feb 24 '12 at 12:05
  • Avoid driving your user nuts by only showing the top sliver of a control without any way for her to get to it. Just hide controls that are inaccessible in a Resize event handler. The Panel class does the logical next thing and hides the scrollbar. – Hans Passant Feb 24 '12 at 12:28
  • @Hans I don't hide the controls because I have 2 panels (splitcontainer). panel1 needs to scroll vertical and panel2 horizontal. Panel2 has a very large calanderview. When scrolling vertical on panel1 makes panel2 also scroll vertical. – Klaasvaak Feb 24 '12 at 12:41

4 Answers4

5

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

mypanel.AutoScroll = false;
mypanel.VerticalScroll.Enabled = false;
mypanel.VerticalScroll.Visible = false;
mypanel.VerticalScroll.Maximum = 0;
mypanel.AutoScroll = true;
kamgman
  • 744
  • 6
  • 9
2

you can try

panel.AutoScroll = true;
panel.HorizontalScroll.Enabled=false;
panel.HorizontalScroll.Visible = false;
CuriousMind
  • 118
  • 1
  • 1
  • 10
1

It works using this code in my panel:

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        ShowScrollBar(this.Handle, 1, false);
        base.WndProc(ref m);
    }

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);
Klaasvaak
  • 5,634
  • 15
  • 45
  • 68
0

If your panel does not perform AutoScroll, you can use its VScroll property:

yourPanel.VScroll = false;
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • If I am not using autoscroll then how am I able to scroll horizontal? – Klaasvaak Feb 24 '12 at 09:57
  • You could be handling the `Scroll` event yourself. However, I just noticed the documentation for `ScrollableControl` contradicts the one for `Panel`, as it says `VScroll` has an effect even if `AutoScroll` is `true`. So, it might work in your case, did you try it? – Frédéric Hamidi Feb 24 '12 at 10:02
  • My panels autoscroll is set to true and the VScroll is set to false. I can still scroll both ways. – Klaasvaak Feb 24 '12 at 10:19
  • HScroll and VScroll won't work if autoscroll is on: "Note AutoScroll maintains the visibility of the scrollbars automatically. Therefore, setting the HScroll or VScroll properties to true have no effect when AutoScroll is enabled." source: http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol.hscroll.aspx – Klaasvaak Feb 24 '12 at 10:23
  • 1
    Indeed, but the documentation for [the class itself](http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol.aspx) says `To manually override which scroll bars are visible, set the VScroll and HScroll properties. If either property is set to false, the corresponding scroll bar is not visible, even if the AutoScroll property is set to true.` Looks like a documentation bug, though. – Frédéric Hamidi Feb 24 '12 at 10:27
  • I reported it on the ScrollableControl Class page. (see bottom) – Klaasvaak Feb 24 '12 at 10:51