获取字符文本更改

时间:2015-11-24 08:14:04

标签: c# devexpress

如何在RichEditControl中获取该位置之前的角色 例如,A在插入符号之前,返回字符串应为A

在文本更改中我需要获得插入位置

1 个答案:

答案 0 :(得分: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?

希望得到这个帮助。

相关问题