方法搜索RichTextBox并突出显示该特定单词的所有实例

本文关键字:单词 实例 RichTextBox 搜索 显示 方法 | 更新日期: 2023-09-27 18:19:39

我真的不知道从哪里开始。

我有一个WPF应用程序,它有一个RichTextBox,里面有一堆使用FlowDocument的文本,它会根据用户的选择而变化。

我需要一种方法,用户可以在TextBox中键入一个单词,如果找到了这个单词的每个实例,就会在相邻的RichTextBox中突出显示。http://kentb.blogspot.com/2009/06/search-and-highlight-text-in-arbitrary.html这个想法是完美的,但我不知道如何将它应用于RichTextBox的应用程序。

提前谢谢!

方法搜索RichTextBox并突出显示该特定单词的所有实例

您尝试过使用RegularExpressions吗?

类似于:

private void searchButton_Click(object sender, EventArgs e)
{
    //Select all text and bring it back to default color values so you
    //can make a new search selection
    richTextBox1.SelectAll();
    richTextBox1.SelectionColor = System.Drawing.Colors.Black;
    //Deselect all text to ready selections
    richTextBox1.DeselectAll();
    //Create a MatchList variable and initialize it to all matches
    //within the RichTextBox. Add a using statement of 
    //System.Text.RegularExpressions 
    Color evenColor = Color.Red;
    Color oddColor = Color.Blue;
    MatchCollection matches = Regex.Matches(richTextBox1.Text,  searchTextBox.Text);
    //Apply color to all matching text
    int matchCount = 0;
    foreach (Match match in matches)
    {
        richTextBox1.Select(match.Index, match.Length);
        //richTextBox1.SelectionColor = System.Drawing.Color.Red;
        richTextBox1.SelectionColor = 
            matchCount++ % 2 == 0 ? evenColor : oddColor;
    }
}

只要你的盒子里不需要同时有多种颜色,这个方法就可以了。我相信,如果有一些额外的逻辑,你也可以将其纳入其中。

edit:在WPF中不起作用。为WinForms保持帖子。

我用FlowDocument来完成。此示例列出了具有该颜色背景的颜色。我使用FlowDocumentReader来显示FlowDocument,但我认为RichTextBox也将显示FlowDocument。这可能看起来有点复杂,但标记实际文本的问题要比必须突出显示一个位置(比如我必须使用Windows.Form RichTextBox)小得多。这是我用来决定什么颜色的高亮显示看起来最好的代码。

docFlowDocument = new FlowDocument();           
System.Windows.Media.Brush defaultBrush = System.Windows.Media.Brushes.White;
docFlowDocument.Background = defaultBrush;
System.Windows.Media.Brush curBrush = defaultBrush;
Paragraph p = new Paragraph();
Run r = new Run();
r.Background = curBrush;
#region nullDocument
 if (String.IsNullOrEmpty(DocText))
 {
     r.Foreground = System.Windows.Media.Brushes.Red;
     r.Text = "No Text";
     p.Inlines.Add(r);
     docFlowDocument.Blocks.Add(p);

     List<string> colorNames = (from pc in typeof(Brushes).GetProperties()
                                    select pc.Name).ToList();
     //Debug.WriteLine(colorNames.Count.ToString());
     //Debug.WriteLine(colorNames[0]);
     Type brushesType = typeof(Brushes);
     System.Reflection.MemberInfo[] membersinfo = brushesType.GetMembers();
     System.Reflection.PropertyInfo[] properties = brushesType.GetProperties();
     for (int i = 0; i < properties.Length; i++)
     {
         r = new Run();
         r.Background = (Brush)properties[i].GetValue(null, null);
         r.Text = colorNames[i];
         p.Inlines.Add(r);
         p.Inlines.Add(new LineBreak());
     }
     docFlowDocument.Blocks.Add(p);
     docFlowDocumentFinishedLastRun = true;
     return docFlowDocument;
 }
#endregion // nullDocument