检测在RichTextBox中的两个TextPointer之间输入文本

时间:2014-11-06 10:20:27

标签: wpf richtextbox

我的WPF程序中有RichTExtBox。想象一下,我有2个TextPointers,表示RTB某些部分的第一个和最后一个。如果输入的文本在这两个指针之间,我怎么知道呢?

//i have the values of these 3 pointer.imagine they are filled
    private bool isBetween(TextPointer first,TextPointer Last,TextPointer Current){
   //i don't know how to write this function
}
上面的代码First中的

Last表示该区域!我希望这个函数告诉我这两个TextPointers之间是否Current

2 个答案:

答案 0 :(得分:0)

尝试:

private bool isBetween(TextPointer first,TextPointer last,TextPointer current)
{
    bool isAfterFirst = first.GetOffsetToPosition(current) >= 0;
    bool isBeforeLast = last.GetOffsetToPosition(current) <= 0;

    return isAfterFirst && isBeforeLast;
}

添加您认为合适的所有空检查......

基于: MSDN : GetOffsetToPosition

答案 1 :(得分:0)

您可以从2 TextRange创建TextPointers并使用方法Contains。这很容易:

private bool isBetween(TextPointer first,TextPointer last,TextPointer current){
   return new TextRange(first, last).Contains(current);
}