方法通过一个RichTextBox搜索和突出显示特定单词的所有实例

问题描述 投票:1回答:2

我用,我应该开始启动这个真的一无所知。

我有具有RichTextBox WPF应用程序,这里面有使用其变化取决于用户的选择FlowDocument文本的负载。

我需要从该用户可以键入一个字转换成TextBox和这个词如果发现然后将与相邻RichTextBox被突出显示的每个实例的方法。 http://kentb.blogspot.com/2009/06/search-and-highlight-text-in-arbitrary.html这个想法是完美的,但我无能,如何将它应用到我的一个RichTextBox应用。

先感谢您!

c# wpf richtextbox flowdocument
2个回答
3
投票

您可以选择使用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;
    }
}

只要你不同时需要你的盒子多种颜色,这种方法可行。有了你,可以纳入一些额外的逻辑,我也敢肯定。

编辑:在WPF不起作用。保持张贴了的WinForms。


0
投票

我用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
© www.soinside.com 2019 - 2024. All rights reserved.