做一个简单的搜索功能,让光标跳转到(或高亮)搜索到的词 [英] making a simple search function, making the cursor jump to (or highlight) the word that is searched for

查看:29
本文介绍了做一个简单的搜索功能,让光标跳转到(或高亮)搜索到的词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用了太长时间,试图找出一个我认为没有那么难的问题.

I have now used way too long time, trying to figure out a problem, which I didn't think would be that hard.

这是交易:

我正在使用 C# 和 WPF 编写一个小型应用程序.

I am writing a small application using C# and WPF.

我有一个包含 FlowDocument 的 RichTextBox.

I have a RichTextBox containing a FlowDocument.

我在富文本框下方添加了一个小文本框和一个按钮.

I have added a small textbox and a button below my richtextbox.

然后用户输入他/她想要搜索的词,然后按下按钮.

The user then types in the word he/she wishes to search for, and presses the button.

然后富文本框将跳转到该词的第一次出现处.

The richtextbox will then jump to the first occurrance of that word.

它只是跳转到正确的行就足够了——它还可以选择、突出显示或放置光标所在的词——任何事情都可以,只要 RichTextBox 滚动到这个词.

it is enough that it just jumps to the correct line - it can also select, highlight or place the cursor by the word - anything will do, as long as the richTextBox is scrolled to the word.

继续按下按钮,将跳转到下一次出现的单词,依此类推,直到文档结束.

Continuing to press the button, will then jump to the next occurance of the word, and so on so forth, till the end of the document.

正如我所说 - 我认为这是一项简单的任务 - 但是我在解决这个问题时遇到了严重的问题.

As I said - I thought it to be a simple task - however I am having serious problems figuring this out.

推荐答案

这应该可以:

public bool DoSearch(RichTextBox richTextBox, string searchText, bool searchNext)
{
  TextRange searchRange;

  // Get the range to search
  if(searchNext)
    searchRange = new TextRange(
        richTextBox.Selection.Start.GetPositionAtOffset(1),
        richTextBox.Document.ContentEnd);
  else
    searchRange = new TextRange(
        richTextBox.Document.ContentStart,
        richTextBox.Document.ContentEnd);

  // Do the search
  TextRange foundRange = FindTextInRange(searchRange, searchText);
  if(foundRange==null)
    return false;

  // Select the found range
  richTextBox.Selection.Select(foundRange.Start, foundRange.End);
  return true; 
}

public TextRange FindTextInRange(TextRange searchRange, string searchText)
{
  // Search the text with IndexOf
  int offset = searchRange.Text.IndexOf(searchText);
  if(offset<0)
    return null;  // Not found

  // Try to select the text as a contiguous range
  for(TextPointer start = searchRange.Start.GetPositionAtOffset(offset); start != searchRange.End; start = start.GetPositionAtOffset(1))
  {
    TextRange result = new TextRange(start, start.GetPositionAtOffset(searchText.Length);
    if(result.Text == searchText)
      return result;
  }
  return null;
}

FindTextInRange 中 for() 循环的原因不幸的是 range.Text 去掉了非文本字符,所以在某些情况下 IndexOf 计算的偏移量会稍微过低.

The reason for the for() loop in FindTextInRangeUnfortunately the range.Text strips out non-text characters, so in some cases the offset computed by IndexOf will be slightly too low.

这篇关于做一个简单的搜索功能,让光标跳转到(或高亮)搜索到的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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