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

Re: Caret on left side of selected text in textbox by Herfried

Herfried
Wed Sep 10 14:31:51 CDT 2003

Hello,

"Ulrich Sprick" <nospam.ulrich.sprick@gmx.de> schrieb:
> 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}")
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet



Re: Caret on left side of selected text in textbox by Ulrich

Ulrich
Thu Sep 11 04:24:31 CDT 2003

Works! Great! Thanks!
ulli

"Herfried K. Wagner [MVP]" <hirf.nosp@m.activevb.de> schrieb im Newsbeitrag news:#MZbgJ9dDHA.904@TK2MSFTNGP11.phx.gbl...
> Hello,
>
> "Ulrich Sprick" <nospam.ulrich.sprick@gmx.de> schrieb:
> > 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}")
> ///
>
> --
> Herfried K. Wagner
> MVP · VB Classic, VB.NET
> http://www.mvps.org/dotnet
>
>



Re: Caret on left side of selected text in textbox by Ulrich

Ulrich
Thu Sep 11 05:42:11 CDT 2003

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


"Herfried K. Wagner [MVP]" <hirf.nosp@m.activevb.de> schrieb im Newsbeitrag news:#MZbgJ9dDHA.904@TK2MSFTNGP11.phx.gbl...
> Hello,
>
> "Ulrich Sprick" <nospam.ulrich.sprick@gmx.de> schrieb:
> > 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}")
> ///
>
> --
> Herfried K. Wagner
> MVP · VB Classic, VB.NET
> http://www.mvps.org/dotnet
>
>



Re: Caret on left side of selected text in textbox by Kevin

Kevin
Sat Sep 13 07:50:14 CDT 2003

"Ulrich Sprick" <ulrich.sprick@gmx.de> wrote in message
news:bjpji4$lve26$1@ID-126690.news.uni-berlin.de...
> 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.

--
Kevin Westhead



Re: Caret on left side of selected text in textbox by Ulrich

Ulrich
Mon Sep 15 07:17:21 CDT 2003

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

"Kevin Westhead" <kevinw@NOSPAM.i2DOTcoDOTuk> schrieb im Newsbeitrag news:#QoqUWfeDHA.1736@TK2MSFTNGP12.phx.gbl...
> "Ulrich Sprick" <ulrich.sprick@gmx.de> wrote in message
> news:bjpji4$lve26$1@ID-126690.news.uni-berlin.de...
> > 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.
>
> --
> Kevin Westhead
>
>



Re: Caret on left side of selected text in textbox by Kevin

Kevin
Mon Sep 15 08:34:39 CDT 2003

"Ulrich Sprick" <ulrich.sprick@gmx.de> wrote in message
news:bk4akh$ontkv$1@ID-126690.news.uni-berlin.de...
> 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>

--
Kevin Westhead