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

C# 方法搜索RichTextBox并突出显示该特定单词的所有实例,c#,wpf,richtextbox,flowdocument,C#,Wpf,Richtextbox,Flowdocument,我真的不知道从哪里开始 我有一个WPF应用程序,它有一个RichTextBox,其中有一个使用FlowDocument的文本负载,它根据用户的选择而变化 我需要一个方法,用户可以从中键入一个字到一个文本框,如果找到这个字的每个实例,将在相邻的RichTextBox中突出显示。这个想法很完美,但我不知道如何使用RichTextBox将其应用到我的应用程序中 提前谢谢你 我用一个FlowDocument来做。此示例列出了具有该颜色背景的颜色。我使用FlowDocumentReader来显示FlowD

我真的不知道从哪里开始

我有一个WPF应用程序,它有一个RichTextBox,其中有一个使用FlowDocument的文本负载,它根据用户的选择而变化

我需要一个方法,用户可以从中键入一个字到一个文本框,如果找到这个字的每个实例,将在相邻的RichTextBox中突出显示。这个想法很完美,但我不知道如何使用RichTextBox将其应用到我的应用程序中

提前谢谢你

我用一个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

你试过使用正规的表达方式吗

比如:

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;
    }
}
只要您不需要同时在方框中使用多种颜色,此方法就可以工作。我相信,再加上一些额外的逻辑,你也可以把它结合起来

编辑:在WPF中不起作用。保持对WinForms的跟踪