John Willemse
John Willemse

Reputation: 6698

TextBox with vertical scrollbar on the left side

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.

Upvotes: 2

Views: 4189

Answers (2)

LarsTech
LarsTech

Reputation: 81675

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

Upvotes: 3

user1728221
user1728221

Reputation:

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

Upvotes: 1

Related Questions