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

发布于 2024-12-25 05:27:56 字数 481 浏览 2 评论 0原文

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

我有一个带有 RichTextBox 的 WPF 应用程序,其中有大量使用 FlowDocument 的文本,该文本会根据用户的选择而变化。

我需要一种方法,用户可以在 TextBox 中输入单词,并且如果找到该单词的每个实例,则将在相邻的 RichTextBox 中突出显示。 http://kentb.blogspot.com/2009 /06/search-and-highlight-text-in-任意.html 这个想法很完美,但我对如何使用 RichTextBox 将其应用到我的应用程序一无所知。

先感谢您!

I'm really clueless with where I should begin to start with this.

I have a WPF application which has a RichTextBox, inside this there is a load of text using a FlowDocument which changes depending upon the user's selection.

I need a method from which a user can type a word into a TextBox and every instance of this word if it is found will then be highlighted with in the adjacent RichTextBox. http://kentb.blogspot.com/2009/06/search-and-highlight-text-in-arbitrary.html This idea would be perfect but I am clueless as how to apply it to my application with a RichTextBox.

Thank you in advance!

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

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

发布评论

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

评论(2

清风无影 2025-01-01 05:27:56

您尝试过使用正则表达式吗?

类似于:

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。

Have you tried using RegularExpressions?

Something like:

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

As long as you don't need multiple colors in your box at the same time, this method works. With some extra logic you could incorporate that, too, I'm sure.

edit: doesn't work in WPF. Keeping post up for WinForms.

我很坚强 2025-01-01 05:27:56

我使用 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

I do it with a FlowDocument. This sample lists the colors with a background of that color. I use FlowDocumentReader to display the FlowDocument but I think a RichTextBox will also display a FlowDocument. It might seem a little complex but marking up the actual text is way less problematic than having to highlight a position like I had to back with Windows.Form RichTextBox. This is code I used to decide what color highlight looked the best.

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