3

How do I get the (autoscroll) vertical scrollbar to be on the left in a System.Windows.Forms.Panel?

Note: I tried modifying the window style ala the textbox question and it did not work.

I tried by subclassing Panel and pinvoking in the ctor, setting CreateParams.Style in the ctor, and by overriding CreateParams getter to tweak the style. no go.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
CoderBrien
  • 663
  • 1
  • 7
  • 22
  • Possible duplicate of [TextBox with vertical scrollbar on the left side](http://stackoverflow.com/questions/14402428/textbox-with-vertical-scrollbar-on-the-left-side) – Broots Waymb Feb 16 '17 at 15:41
  • not a dup, already tried that to no avail. – CoderBrien Feb 16 '17 at 15:44
  • There's where they like it in some places of this world, like countries in the Middle-East. They read their alphabet right-to-left. Set the panel's RightToLeft property to Yes. – Hans Passant Feb 16 '17 at 16:09
  • @HansPassant Since `RightToLeft` property is an ambient property, setting `RightToLeft` property of the panel may be annoying. But it's technically correct. – Reza Aghaei Feb 16 '17 at 16:18
  • @HansPassant `RightToLeft` worked! although i don't want the child controls to inherit it, but i'm sure i can work that out. If you create an answer I will accept it. – CoderBrien Feb 16 '17 at 16:30
  • They already inherit it, as Reza noted. Fwiw: users that like the scrollbar on the left also like to have the window frame right-to-left. So setting the property on the form is the more sensible approach. – Hans Passant Feb 16 '17 at 16:38

1 Answers1

6

If you add WS_EX_LEFTSCROLLBAR extended style to the control it shows scrollbar on left side:

using System.Windows.Forms;
public class ExPanel : Panel
{
    private const int WS_EX_LEFTSCROLLBAR = 0x00004000;
    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            cp.ExStyle = cp.ExStyle | WS_EX_LEFTSCROLLBAR;
            return cp;
        }
    }
}

Also keep in mind, Setting RightToLeft property to Yes will do the trick for you, but since the RightToLeft property is an ambient property, then all children of the panel will also inherit that value and will be right to left. What I have shared here in this answer is just showing scrollbar at left side without affecting RightToLeft.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • are you sure? as i said in the post I tried this and it didn't work for me. – CoderBrien Feb 16 '17 at 16:27
  • Copy and paste the the class to your solution and build the solution. Then drop an instance of `ExPanel` to your from, set its `AutoScroll` to true and then by adding some contents make the scrollbar visible. Run the application and see the result. – Reza Aghaei Feb 16 '17 at 16:29
  • Also don't miss what Has said, also my comment. – Reza Aghaei Feb 16 '17 at 16:30
  • yeah, like i said. i tried it and it didn't work. did you actually test the code? – CoderBrien Feb 16 '17 at 16:30
  • Yes, follow the scenario which I said in above comment. – Reza Aghaei Feb 16 '17 at 16:31
  • Downvoting the answer without testing it, is really strange! Is it not what someone can expect when tries to help another one! – Reza Aghaei Feb 16 '17 at 16:33
  • doh.... i was setting `.Style` instead of `.ExStyle`. perfect! this solution has the advantage that it doesn't affect the children controls (which is what i need)... gracias. – CoderBrien Feb 16 '17 at 16:34
  • You're welcome, don't be hurried in downvoting answers. It's better to just downvote totally wrong or misleading answers. You can ask more question about answers from the one who posted, specially when some experienced user answered your question :) – Reza Aghaei Feb 16 '17 at 16:39