【问题标题】:Highlighting strings in richtextBox突出显示richtextBox中的字符串
【发布时间】:2012-08-21 03:02:54
【问题描述】:

我需要突出显示 .xls 文件中列出的richtextBox 中的所有单词,这是我的代码的一部分:

public void HighlightWords(RichTextBox rtb1, DataTable dtXLS)
{
    for (int i = 0; i < dtXLS.Rows.Count; i++)
    {
        string[] wordsToRedact = new string[dtXLS.Rows.Count];

        wordsToRedact[i] = dtXLS.Rows[i][0].ToString();

        Regex test = new Regex(@"[\p{P}|\s](" + wordsToRedact[i] + @")[\p{P}|\s]", RegexOptions.Singleline | RegexOptions.Compiled);
        MatchCollection matchlist = test.Matches(rtb1.Text);

        if (matchlist.Count > 0)
        {
            for (int j = 0; j < matchlist.Count; j++)
            {
                WordsToRedact words = new WordsToRedact(matchlist[j]);
                HighLighting highLight = new HighLighting();
                highLight.Highlight_Words(rtb1, words);
            }
        }
    }
}

class HighLighting
{
    public void Highlight_Words(RichTextBox rtb, WordsToRedact e)
    {
        rtb.SelectionBackColor = Color.LightSkyBlue;
        rtb.SelectionStart = e.index;
        rtb.SelectionLength = e.length;

        rtb.ScrollToCaret();
    }
}

class WordsToRedact
{
    public int index;
    public int length;
    public string value;

    public WordsToRedact(Match m)
    {
        this.index = m.Groups[1].Index;
        this.length = m.Groups[1].Length;
        this.value = m.Groups[1].Value;
    }   
}

问题是,它没有突出显示一些也与正则表达式匹配的单词。有些被突出显示,但有些没有。准确性是我的问题,我不知道我哪里错了。

【问题讨论】:

    标签: c# .net regex string richtextbox


    【解决方案1】:

    我检查了您的代码,其中有一些问题,我将它们列在下面:
    第一个:

    for (int i = 0; i < dtXLS.Rows.Count; i++)
    {
        string[] wordsToRedact = new string[dtXLS.Rows.Count];
        ...  
    

    错了,你应该在for循环之前初始化你的字符串数组,否则它会在每次循环迭代中被更新,这样做:

    string[] wordsToRedact = new string[listBox1.Items.Count];
    for (int i = 0; i < dtXLS.Rows.Count; i++)
    {
       ...
    

    第二个:(你的主要问题)
    您应该在选择后而不是之前为所选部分着色,这就是您的代码不选择最后一个匹配项的原因,您应该这样做:

    rtb.SelectionStart = e.index;
    rtb.SelectionLength = e.length;
    rtb.SelectionBackColor = Color.LightSkyBlue;
    

    最后一个:(有疑问) 我想,但我不确定你应该使用索引零 [0] 而不是 [1]

    public WordsToRedact(Match m)
    {
        this.index = m.Groups[0].Index;
        this.length = m.Groups[0].Length;
        this.value = m.Groups[0].Value; 
    }
    

    【讨论】:

    • 我会试试这个,虽然我已经解决了我的问题,但是你的回答太棒了,因为你列举了我所有的编码问题,非常感谢!
    • 我将您的答案标记为正确答案,因为它也解决了问题。非常感谢!除了您的回答,我还将发布另一个我也尝试过并成功解决我的问题的选项!再次感谢!
    • @jomskie19 很高兴我的朋友。
    【解决方案2】:

    这将起作用:

    Regex test = new Regex(@"\b(" + wordsToRedact[i] + @")\b", 
    RegexOptions.Singleline | RegexOptions.Compiled);
    

    【讨论】:

    • 我也试过了,但还是漏掉了一些需要高亮的字。
    • 原版没有找到正文中的第一个单词。我纠正了它。但我不知道还有什么可能。可以是任何东西。试着举个例子。
    【解决方案3】:

    mahdi-tahsildari 的回答确实回答了我的问题!但除了他的回答之外,我还想发布我尝试过的另一个选项,它也解决了这个问题。

    我将 HighLighting 类更改为以下代码:

    class HighLighting
    {
        public void HighlightText(RichTextBox rtb, string word)
        {
            int s_start = rtb.SelectionStart, startIndex = 0, index;
    
            while ((index = rtb.Text.IndexOf(word, startIndex)) != -1)
            {
                rtb.Select(index, word.Length);
                rtb.SelectionBackColor = Color.Yellow;
    
                startIndex = index + word.Length;
            }
    
            rtb.SelectionStart = s_start;
            rtb.SelectionLength = 0;
            rtb.SelectionColor = Color.Black;
            rtb.ScrollToCaret();
        }
    }
    

    一切顺利。这段代码和 mahdi-tahsildari 的回答做了同样的事情!感谢你的帮助! :))

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 2013-10-21
      • 2015-06-24
      • 2012-09-09
      相关资源
      最近更新 更多