2015-11-24 33 views
1

如何在RichEditControl 的位置,例如之前获得字符,A是尖前,返回的字符串应该是A获取字符文本更改

和文本中的变化,我需要得到插入位置

回答

1

您可以使用RichEditControl1.Document.CaretPosition属性获取当前位置,然后仅使用string.Substring(0, position.ToInt())获取插入符当前位置之前的字符串。

检查下面的代码片段:

private void simpleButton1_Click(object sender, EventArgs e) 
{ 
    DevExpress.XtraRichEdit.API.Native.DocumentPosition position = richEditControl1.Document.CaretPosition; 
    if (richEditControl1.Document.Text.Length > 0) 
    { 
     //Returns all previous text befor the caret 
     XtraMessageBox.Show(richEditControl1.Document.Text.Substring(0, position.ToInt())); 
     int intPosition = position.ToInt(); 
     if (intPosition > 0 && intPosition < richEditControl1.Document.Length) 
     { 
      //It will return previous character 
      XtraMessageBox.Show(richEditControl1.Document.Text.Substring(intPosition - 1, 1)); 
     } 
    } 
} 

参考文献:
How to get RepositoryItemRichEdit caret position
How to get/set the caret position within the editor?

希望这有助于。