3

I am trying to "scroll" a single line textbox to the left while some text is selected, without changing the selection.

enter image description here

In above picture I have a textbox A with selection of letters M to Z, but I cannot see the letters at the beginning of the textbox.

Textbox B is what I did manually by dragging the selection in reverse from Z to M, which scrolls the visible area so the letters to the left become visible.

By using the code below I can scroll to the beginning, but I'll lose the selection.

  txtTest.SelectionStart = 0;
  txtTest.SelectionLength = 0;
  txtTest.ScrollToCaret();

How can I programmatically select M to Z while still showing the first characters in the textbox?
(like in picture B above)

John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

0

Untested, but when you call that piece of code in your post, you could record where the caret currently is. Then enable that selection again.

int originalSelectionStart = txtTest.SelectionStart;
int originalSelectionLength = txtTest.SelectionLength;
txtTest.SelectionStart = 0;
txtTest.SelectionLength = 0;
txtTest.ScrollToCaret();
txtTest.SelectionStart = originalSelectionStart;
txtTest.SelectionLength = originalSelectionLength;
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • I just tested your code, but it seems that the Textbox automatically moves the visible area to the end of the selection. This "scrolls" the first few letters out of view again. – user1751517 Oct 17 '12 at 07:02