The documentation states that you can use the SelectionStart property of the
TextBox to get the position of the caret in the TextBox. Well, what if the
selection is BEFORE the caret?

Say you have the word â??TEXTâ?? and you want to select â??EXâ??. There are 2 ways
you can do this.
You can click before the E and drag to the X so the caret is now sitting
after the X.
You can click after the X and drag to the E so the caret is now sitting
before the E.

In both cases the SelectionStart property has the value of 1 and the
SelectionLength has a value of 2, yet the caret is in two different places.
Using the SelectionStart property alone canâ??t always give you the correct
caret position.

How can one get the true caret position and be absolutely sure itâ??s correct?

Re: Get caret pos in TextBox by Tobin

Tobin
Sat Mar 12 19:08:47 CST 2005

"dlgproc" <dlgproc@discussions.microsoft.com> wrote in message
news:E38866C5-36CC-49C6-A65B-55AE913F2CBA@microsoft.com...
> How can one get the true caret position and be absolutely sure it's
> correct?

I struggled with this kind of thing when writing SqlBuddy. I eventually
found a way forward. The following class uses a RichTextBox, but I think it
will work for a TextBox too with a few small changes.

http://cvs.sourceforge.net/viewcvs.py/sqlbuddy/SqlBuddy/Application/src/GUI/RichTextBoxServices.cs?rev=1.1&view=markup

Hope this helps

Tobin