2014-01-30 47 views
0

我想要獲取文本框或RichtextBox中的鍵盤光標位置。在WinFrom我通過使用此代碼如何獲取鍵盤光標位置作爲文本框中的點

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

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

但在WPF我不能讓GetPositionFromCharIndex財產是否有任何其他的方式來實現這一

我想展示在鍵盤光標下的一個列表框(像智能感知這樣做)

任何幫助將欣賞

+0

這可以幫助你 http://stackoverflow.com/questions/17909651/c-sharp-get-cursor-line-in-richtextbox –

回答

1

WPFTextBox你可以插入符位置在多個方面:

  1. 通過訪問TextBox.SelectionStartTextBox.SelectionLength屬性。
  2. TextBox.CaretIndex財產。

不幸的是,在TextBox沒有GetPositionFromCharIndex所以你必須使用GetRectFromCharacterIndex以獲取智能感知出發點做一個小竅門:

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

注:我們使用BottomRight,使放intellisense在適當的地方。

+0

我想點值,這樣我就可以這樣 在Y軸上增加值'pY + =(int)rtb.Font.GetHeight()* 2;' –

+0

@UzairAli更新;) –

+0

如何設置 'listbox.Location'屬性在WPF –

相關問題