Caret on left side of selected text in textbox

  • Thread starter Thread starter Ulrich Sprick
  • Start date Start date
U

Ulrich Sprick

Hi all,

how can select some text in the textbox, but have the caret on the LEFT side of the selection? You can do that with the mouse by
drawing the mouse cursor from right to left over some text. But how can it be done programmatically?

I played around with textbox.SelectionStart and .SelectionLength, and .Select(pos, len), but negative values for SelectionLength
throw an exception. Do I have to send a WM_ or EM_ message to the window handle?

Thanks in advance,
ulrich
 
Hello,

Ulrich Sprick said:
how can select some text in the textbox, but have the caret
on the LEFT side of the selection? You can do that with
the mouse by drawing the mouse cursor from right to left
over some text. But how can it be done programmatically?

Quick and *very* dirty:

\\\
Me.TextBox1.Focus()
Me.TextBox1.SelectionStart = 6
Me.TextBox1.SelectionLength = 0
SendKeys.Send("+{LEFT 4}")
///
 
Works! Great! Thanks!
ulli

Herfried K. Wagner said:
Hello,



Quick and *very* dirty:

\\\
Me.TextBox1.Focus()
Me.TextBox1.SelectionStart = 6
Me.TextBox1.SelectionLength = 0
SendKeys.Send("+{LEFT 4}")
///
 
Hi Herfried,

w32api.SendMessage( this.TextBox1.Handle, (int)w32api.EditControlMsg.EM_SETSEL, 7, 3 );

should place the "anchor point" at pos 7 and the caret at pos 3, according to the W32 SDK, but either the this wrong or the DotNET
framework adds another level of logik to the framework textbox: The caret is still on the right hand side of the selection.

Thus, your hack seems to be the only working solution.
Thanks,
ulli
 
Ulrich Sprick said:
w32api.SendMessage( this.TextBox1.Handle,
(int)w32api.EditControlMsg.EM_SETSEL, 7, 3 );
should place the "anchor point" at pos 7 and the caret at pos 3, according
to the W32 SDK, but either the this wrong or the DotNET
framework adds another level of logik to the framework textbox: The caret
is still on the right hand side of the selection.


You could try using EM_POSFROMCHAR to get the x/y location of a specified
character index and then use SetCaretPos to move the caret to that position.
SetCaretPos might require some PointToClient or PointToScreen conversions,
but I've never used this function so I can't say for sure.
 
Hi Kevin,
the documentation for SetCaretPos() doesn't make statements regarding side effects on the selected text, so this might work. I'll
give it a try and post the results here (in a few days).
Thank you,
ulli
 
Ulrich Sprick said:
the documentation for SetCaretPos() doesn't make statements regarding side
effects on the selected text, so this might work. I'll
give it a try and post the results here (in a few days).


You're right of course, changing the caret position could clear the
selection. I guess there's only one way to find out. <g>
 
Back
Top