简体   繁体   中英

How to get Keyboard cursor position as Point in textbox

i want to get Keyboard cursor position in textbox or RichtextBox. On WinFrom i did this by using this code

Point p = rtb.GetPositionFromCharIndex(rtb.SelectionStart);

p.Y += (int)rtb.Font.GetHeight()*2;               
lstboxIntelli.Location = p;
lstboxbIntelli.Show();
ActiveControl = lstboxIntelli;

But in WPF i cant get GetPositionFromCharIndex property is there any other way to achieved this

I want to position a Listbox right under the keyboard cursor (Like Intellisense)

Any help will be appreciate

In WPF TextBox you can get caret position in multiple ways:

  1. By accessing TextBox.SelectionStart and TextBox.SelectionLength properties.
  2. TextBox.CaretIndex property.

Unfortunately there is no GetPositionFromCharIndex in TextBox so you'll have to do a little trick using GetRectFromCharacterIndex to get starting point for intellisense:

var rect = rtb.GetRectFromCharacterIndex(rtb.CaretIndex);
var point = rect.BottomRight;

Note: We are using BottomRight to make put intellisense in proper place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM