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

前端 未结 7 1707
粉色の甜心
粉色の甜心 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:04

    Try this.

    Under `Control.KeyPress` event,

    I use Control.GetPositionFromCharIndex(Control.SelectionStart) to get the caret position which relate to the control. Then translate this position relate to the screen for the form's new location.

    private void aTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        SuperComboForm supcomboF = new SuperComboForm();
        supcomboF.StartPosition = FormStartPosition.Manual;
        var caret_pos = aTextBox.GetPositionFromCharIndex(aTextBox.SelectionStart);
        supcomboF.Location = PointToScreen(new Point(caret_pos.X, caret_pos.Y));
        supcomboF.ShowDialog();
    }
    

提交回复
热议问题