如何找到一个TextRange RichTextBox中(二TextPointers之间) [英] How to find a TextRange in RichTextBox (between two TextPointers)

查看:275
本文介绍了如何找到一个TextRange RichTextBox中(二TextPointers之间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我System.Windows.Controls.RichTextBox,我想找到一个给定单词的一个TextRange。然而,这是不是给我的第一句话发现正确后PositionAtOffset。第一个是正确的,然后为下发现的话,位置是不正确的。我使用了正确的方法是什么?



通过listOfWords循环

 字= listOfWords [J]的ToString(); 

startPos =新的TextRange(transcriberArea.Document.ContentStart,transcriberArea.Document.ContentEnd).Text.IndexOf(Word.Trim());

leftPointer = textPointer.GetPositionAtOffset(startPos + 1,LogicalDirection.Forward);

rightPointer = textPointer.GetPositionAtOffset((startPos + 1 + Word.Length),LogicalDirection.Backward);
的TextRange myRange =新的TextRange(leftPointer,rightPointer);


解决方案

此代码改编自样品在的MSDN 会发现在的话从一个指定的位置。

 的TextRange FindWordFromPosition(TextPointer位置,串字)
{
,而(位置!= NULL)
{
如果(position.GetPointerContext(LogicalDirection.Forward)== TextPointerContext.Text)
{
串textRun = position.GetTextInRun(LogicalDirection.Forward);

//查找匹配字子任的起始索引。
INT indexInRun = textRun.IndexOf(字);
如果(indexInRun> = 0)
{
TextPointer开始= position.GetPositionAtOffset(indexInRun);
TextPointer结束= start.GetPositionAtOffset(word.Length);
返回新的TextRange(开始,结束);
}
}

位置= position.GetNextContextPosition(LogicalDirection.Forward);
}

//位置将是无效的,如果未找到字。
返回NULL;
}

您可以使用它像这样:

 的String [] = listOfWords新的字符串[] {字,文本,ETC,}; 
为(INT J = 0; J< listOfWords.Length; J ++)
{
串字= listOfWords [J]的ToString();
的TextRange myRange = FindWordFromPosition(x_RichBox.Document.ContentStart,字);
}


In my System.Windows.Controls.RichTextBox, I would like to find a TextRange of a given word. However, it is not giving me the correct PositionAtOffset after the first found word. The first one is correct, and then for the next found words, the position is not correct. Am I using the correct methods?

Loop through listOfWords

Word= listOfWords[j].ToString();

startPos = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text.IndexOf(Word.Trim());

 leftPointer = textPointer.GetPositionAtOffset(startPos + 1, LogicalDirection.Forward);

rightPointer = textPointer.GetPositionAtOffset((startPos + 1 + Word.Length), LogicalDirection.Backward);
TextRange myRange= new TextRange(leftPointer, rightPointer);

解决方案

This code adapted from a sample at MSDN will find words in from a specified position.

TextRange FindWordFromPosition(TextPointer position, string word)
{
    while (position != null)
    {
        if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
        {
            string textRun = position.GetTextInRun(LogicalDirection.Forward);

            // Find the starting index of any substring that matches "word".
            int indexInRun = textRun.IndexOf(word);
            if (indexInRun >= 0)
            {
                TextPointer start = position.GetPositionAtOffset(indexInRun);
                TextPointer end = start.GetPositionAtOffset(word.Length);
                return new TextRange(start, end);
            }
        }

        position = position.GetNextContextPosition(LogicalDirection.Forward);
    }

    // position will be null if "word" is not found.
    return null;
}

You can then use it like so:

string[] listOfWords = new string[] { "Word", "Text", "Etc", };
for (int j = 0; j < listOfWords.Length; j++)
{
    string Word = listOfWords[j].ToString();
    TextRange myRange = FindWordFromPosition(x_RichBox.Document.ContentStart, Word);
}

这篇关于如何找到一个TextRange RichTextBox中(二TextPointers之间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆