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

前端 未结 7 1758
粉色の甜心
粉色の甜心 2021-01-12 08:14

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 spell

7条回答
  •  南方客
    南方客 (楼主)
    2021-01-12 09:03

    Here is my solution:

    private Point GetCaretPosition()
    {
        Point result = TextBox.GetPositionFromCharIndex(TextBox.SelectionStart);
    
    
        if (result.X == 0 && TextBox.Text.Length > 0)
        {
            result = TextBox.GetPositionFromCharIndex(TextBox.Text.Length - 1);
            int s = result.X / TextBox.Text.Length;
            result.X = (int)(result.X + (s * 1.3));
        }
    
    
        return result;
    }
    

提交回复
热议问题