如何在 RichTextBox 中查找 TextRange(两个 TextPointer 之间)

发布于 2024-07-23 21:58:36 字数 620 浏览 5 评论 0原文

在我的 System.Windows.Controls.RichTextBox 中,我想找到给定单词的 TextRange。 但是,在第一个找到的单词之后,它没有给我正确的 PositionAtOffset 。 第一个是正确的,然后对于下一个找到的单词,位置不正确。 我使用的方法正确吗?

循环遍历单词列表

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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2024-07-30 21:58:36

此代码改编自 MSDN 上的示例从指定位置查找单词。

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;
}

然后你可以像这样使用它:

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);
}

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);
}
痕至 2024-07-30 21:58:36

我想,它也应该有效。

这就像从列表中“查找下一个”任何项目。

    TextPointer FindWordsFromPosition(TextPointer position, IList<string> words)
    {
        var firstPosition = position;
        while (position != null)
        {
            if (position.GetPointerContext(LogicalDirection.Forward) ==    TextPointerContext.Text)
            {
                string textRun = position.GetTextInRun(LogicalDirection.Forward);

                var indexesInRun = new List<int>();

                foreach (var word in words)
                {
                    var index = textRun.IndexOf(word);

                    if (index == 0)
                        index = textRun.IndexOf(word, 1);

                    indexesInRun.Add(index);
                }

                indexesInRun = indexesInRun.Where(i => i != 0 && i != -1).OrderBy(i => i).ToList();

                if (indexesInRun.Any())
                {
                    position = position.GetPositionAtOffset(indexesInRun.First());
                    break;
                }

                return firstPosition;
            }
            else
                position = position.GetNextContextPosition(LogicalDirection.Forward);
        }

        return position;
    }

I think, it should work also.

It is like "find next" any item from the list.

    TextPointer FindWordsFromPosition(TextPointer position, IList<string> words)
    {
        var firstPosition = position;
        while (position != null)
        {
            if (position.GetPointerContext(LogicalDirection.Forward) ==    TextPointerContext.Text)
            {
                string textRun = position.GetTextInRun(LogicalDirection.Forward);

                var indexesInRun = new List<int>();

                foreach (var word in words)
                {
                    var index = textRun.IndexOf(word);

                    if (index == 0)
                        index = textRun.IndexOf(word, 1);

                    indexesInRun.Add(index);
                }

                indexesInRun = indexesInRun.Where(i => i != 0 && i != -1).OrderBy(i => i).ToList();

                if (indexesInRun.Any())
                {
                    position = position.GetPositionAtOffset(indexesInRun.First());
                    break;
                }

                return firstPosition;
            }
            else
                position = position.GetNextContextPosition(LogicalDirection.Forward);
        }

        return position;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文