在WPF中的RTF文本框中突出显示关键字 [英] Highlighting keywords in a richtextbox in WPF

查看:72
本文介绍了在WPF中的RTF文本框中突出显示关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序需要浏览一段文本并找出某个关键字的出现次数.它还必须突出显示文本中的每个关键词.

I'm making a program which needs to look through a paragraph of text and find how many times a certain keyword/keywords appear. It also has to highlight each of these key words in the text.

我设法使他成为界面,它现在可以跟踪单词出现的次数,但是我对于如何突出显示关键字出现的位置确实很迷恋.我将在下面发布我的代码,对于如何在Richtextbox中搜索和突出显示文本的任何帮助,我们将不胜感激.由于这是在WPF中,因此显然无法使用richtextbox.find().

I have managed to make he interface and it can now track how many times the word appears but I am really stuck for how to highlight where the keywords appear. I will post my code below, any help is greatly appreciated on how to search for and highlight text inside a richtextbox. Since this is in WPF the obvious richtextbox.find() is not avaliable for use.

class TextAnalyser
{
    public int FindNumberOfOccurances(List<string> keywords, string email)
    {
        int occurances = 0;
        foreach (string keyword in keywords)
        {
            occurances += email.ToUpper().Split(new string[] { keyword.ToUpper() }, StringSplitOptions.None).Count() - 1; 
        }
        return occurances;
    }

    public void TurnTextRed(List<string> keywords, string email, RichTextBox TextBox)
    {
        foreach(string keyword in keywords)
        {
        }
    }

    public List<string> ConvertTextToList(string text)
    {
        char[] splitChars = {};
        string[] ArrayText = text.Split( splitChars, StringSplitOptions.RemoveEmptyEntries);
        return ArrayText.ToList<string>();
    }

    public string GetStringFromTextBox(RichTextBox TextBox)
    {
        var textRange = new TextRange(
            TextBox.Document.ContentStart,
            TextBox.Document.ContentEnd
        );
        return textRange.Text;
    }
}

这是我的主窗口

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void AnalyseButton_Click(object sender, RoutedEventArgs e)
    {
        var textTool = new TextAnalyser();
        var keyWords = textTool.ConvertTextToList(textTool.GetStringFromTextBox(WordTextBox).Trim());
        var email = textTool.GetStringFromTextBox(EmailTextBox).Trim();
        int usesOfWord = textTool.FindNumberOfOccurances(keyWords, email);
        Occurances.Text = usesOfWord.ToString();
    }
}

推荐答案

这里是用于获取Richtextbox文档中所有单词的方法.

Here is the method is used to get all of word in richtextbox's document.

 public static IEnumerable<TextRange> GetAllWordRanges(FlowDocument document)
     {
         string pattern = @"[^\W\d](\w|[-']{1,2}(?=\w))*";
         TextPointer pointer = document.ContentStart;
         while (pointer != null)
         {
             if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
             {
                 string textRun = pointer.GetTextInRun(LogicalDirection.Forward);
                 MatchCollection matches = Regex.Matches(textRun, pattern);
                 foreach (Match match in matches)
                 {
                     int startIndex = match.Index;
                     int length = match.Length;
                     TextPointer start = pointer.GetPositionAtOffset(startIndex);
                     TextPointer end = start.GetPositionAtOffset(length);
                     yield return new TextRange(start, end);
                 }
             }

             pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
         }
     }

您可以更改用于拆分单词的模式.

You can change the pattern which is used to split words.

最后,轻松突出显示您的单词.

At last, easy to highlight your words.

  IEnumerable<TextRange> wordRanges = GetAllWordRanges(RichTextBox.Document);
        foreach (TextRange wordRange in wordRanges)
        {
            if (wordRange.Text == "keyword")
            {
                wordRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
            }
        }

这篇关于在WPF中的RTF文本框中突出显示关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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