Knowing the point location of the caret in a Winforms TextBox?

c#, winforms

Solution

If you don't mind using Win32-API use GetCaretPos to get the exact position.

Problem

I need to know the exact point location in my window where the caret currently resides so that I can pop up a small window under the text (similar to intellisense or a spellchecker). The issue is that GetPositionFromCharIndex doesn't seem to work on TextBoxes. This is what I'm trying to do: ``` Point pt = mTextBox.GetPositionFromCharIndex(mTextBox.SelectionStart); pt.Y = (int)Math.Ceiling(mTextBox.Font.GetHeight()); mListBox.Location = pt; ``` However, GetPositionFromCharIndex always returns (0, 0) for TextBoxes (apparently it isn't implemented like it is for RichTextBox). While this function (and this code) works fine for RichTextBoxes, I don't want to migrate the rest of my code from TextBox to RichTextBox except as an absolute LAST resort. There are so many incompatibilities with RichTextBox/TextBox that I'll have to rewrite a very large chunk of my code to do this. Are there any Winforms wizards out there who know how to do this?

Original source