tags:

views:

128

answers:

2

I'm using a System.Windows.Forms.TextBox. It is possible to select text by using the keyboard in such a way that the caret is positioned at the start of the selection - by holding Shift and moving the caret to the left.

I would like to do the same programmatically.

For example, suppose I have a text box with the text "Some sample text". I would like the "sample" word to be selected, and the caret to be positioned just before the "s" in "sample".

If I do this:

textbox.SelectionStart = 5;
textbox.SelectionLength = 6;

then I get the word selected, but the caret is just after "e" in "sample".

If I do this:

textbox.SelectionStart = 11;
textbox.SelectionLength = -6;

I get an exception.

How can I position the caret at the start of the selection?

+2  A: 

I don't that is possible to program. As it is stated in MSDN native windows controls displays a flashing caret at the end position regardless of the relative values of start and end.

RaYell
Indeed. Thanks for the link, it really does seem to be impossible.
romkyns
A: 

Very few things are impossible...

textbox.select(11, 0);
SendKeys.Send("+{LEFT}+{LEFT}+{LEFT}+{LEFT}+{LEFT}+{LEFT}");
JimG