Caret Position in TextField Controls

How exactly can you find the position of the caret in a TextField? There’s no obvious CaretPosition property or GetCaret method; what’s a developer to do?

The answer is sneaky, but obvious: use the SelectionStart property. This might seem unintuitive, as there is no selection; but in terms of properties, SelectionStart has the caret position, and SelectionLength will be zero.

And this makes sense even when there is a selection.

Sneaky. But obvious once you think about it.

About Ashiq Alibhai, PMP

Ashiq has been coding C# since 2005. A desktop, web, and RIA application developer, he's touched ASP.NET MVC, ActiveRecord, Silverlight, NUnit, and all kinds of exciting .NET technologies. He started C# City in order to accelerate his .NET learning.
This entry was posted in Core .NET, Wndows Forms and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *