2

I'm developing a Windows Forms application in C#, which has a multiline TextBox control on a form.

Due to specific (irrelevant) reasons, this TextBox needs a vertical scrollbar on the left side of the TextBox control. I've off course searched for a solution, but I couldn't find any... so my questions are:

1) Is there a way to make the automatic vertical scrollbar of a TextBox control (or a usercontrol derived from TextBox or TextBoxBase) appear on the left instead of the right? This is the preferred method, since all scolling is then still handled by the control. Since chancing the RightToLeft property for such a TextBox actually moves the scrollbar to the left, I feel there must be a hack to be exploited here.

or

2) Is there a message that I can intercept with my IMessageFilter implementation when the TextBox is scrolled, even though it doesn't have scrollbars? I.e. a user can scroll using the arrow keys and the textbox will move lines up and down, but I can't find any messages fired when that occurs.

Maybe another idea of how to accomplish this?

Edit to add: The text needs to be aligned to the right horizontally! Otherwise I would have solved it already.

New edit as of 11/03/2014: Okay, after BenVlodgi's comment I started having doubts about my own sanity. So I created a test project and now I remember why setting RightToLeft to Yes was not working.

The image below shows a regular TextBox on the left with that setting. The scrollbar is on the left and the text on the right, but the text is not shown properly. The period at the end of the sentence is moved in front of the sentence.

The second TextBox control is the one suggested in LarsTech's answer, which functions correctly and does not move any punctuation.

Difference between the two TextBox controls

Therefore, I accept and reward the bounty to LarsTech's answer.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
  • From memory, if you set `LeftToRight` to Yes, and `TextAlign=HorizontalAlign.Right` for the text, what happens? – dash Jan 18 '13 at 15:47
  • Yes, I've tried that, but then the text flows from right to left (backwards), which is not what I want. – John Willemse Jan 18 '13 at 15:48
  • 1
    see http://stackoverflow.com/questions/8003530/is-it-possible-to-define-a-left-handed-combobox-ie-scrollbar-on-the-left-in-a – Rachel Gallen Jan 18 '13 at 15:52
  • Thanks Rachel, I will take a closer look at that. It seems I can modify that code to work with a textbox. – John Willemse Jan 18 '13 at 15:58
  • @JohnWillemse you said `The text needs to be aligned to the right horizontally` Is this by any change a typo. Setting the RightToLeft Property to `RightToLeft.Yes`, does in fact accomplish both of these tasks. Perhaps you meant to say you want the text to be horizontally aligned to the left. – BenVlodgi Mar 11 '14 at 12:59
  • @BenVlodgi No that was not a typo. The text needs to be aligned to the right. If you read my previous comment: "Yes, I've tried that, but then the text flows from right to left (backwards), which is not what I want." However, when I try this now it works perfectly with `RightToleft.Yes` and `TextAlign.Left`, which then aligns the text right and does not print it backwards anymore. I'm using a newer version of Visual Studio now, could that be a change, otherwise I'm absolutely flabbergasted that it does work without the need of p/invoke now... – John Willemse Mar 11 '14 at 15:09
  • @BenVlodgi I edited my question to reflect on your comment and show the subtle difference. – John Willemse Mar 11 '14 at 15:32
  • @JohnWillemse Okay I see now, when I tested it, I did not use punctuation and saw that the text seemed to be flowing properly. Such a strange behavior. – BenVlodgi Mar 11 '14 at 16:31

2 Answers2

3

I took some of the example code from Rachel Gallen's link and made this version of the TextBox:

public class TextBoxWithScrollLeft : TextBox {
  private const int GWL_EXSTYLE = -20;
  private const int WS_EX_LEFTSCROLLBAR = 16384;

  [DllImport("user32", CharSet = CharSet.Auto)]
  public extern static int GetWindowLong(IntPtr hWnd, int nIndex);

  [DllImport("user32")]
  public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

  protected override void OnHandleCreated(EventArgs e) {
    base.OnHandleCreated(e);
    int style = GetWindowLong(Handle, GWL_EXSTYLE);
    style = style | WS_EX_LEFTSCROLLBAR;
    SetWindowLong(Handle, GWL_EXSTYLE, style);
  }
}

I've never done it before, but the results seem to have worked:

enter image description here

LarsTech
  • 80,625
  • 14
  • 153
  • 225
1

By setting the RightToLeft property true. But it said the content would also be from right to left, so I don't know if that would solve your problem...But that's a way to set the scrollbar on the left hand side.

http://bytes.com/topic/c-sharp/answers/255138-scrollbar-position