突出显示所有搜索的单词

时间:2012-08-07 18:29:17

标签: c# find richtextbox highlight

在我的RichtextBox中,如果我写的如下。

  

这是我的笔,
  他的笔很漂亮。

现在我搜索“是”字样 输出如下。

应突出显示所有“是”。

6 个答案:

答案 0 :(得分:18)

怎么样:

static class Utility {
    public static void HighlightText(this RichTextBox myRtb, string word, Color color) {  

       if (word == string.Empty)
            return;

       int s_start = myRtb.SelectionStart, startIndex = 0, index;

       while((index = myRtb.Text.IndexOf(word, startIndex)) != -1) {
           myRtb.Select(index, word.Length);
           myRtb.SelectionColor = color;

           startIndex = index + word.Length;
       }

       myRtb.SelectionStart = s_start;
       myRtb.SelectionLength = 0;
       myRtb.SelectionColor = Color.Black;
    }
}

答案 1 :(得分:1)

看起来会这样做。

http://www.dotnetcurry.com/ShowArticle.aspx?ID=146

int start = 0;
int indexOfSearchText = 0;

    private void btnFind_Click(object sender, EventArgs e)
    {
        int startindex = 0;

        if(txtSearch.Text.Length > 0)
            startindex = FindMyText(txtSearch.Text.Trim(), start, rtb.Text.Length);

        // If string was found in the RichTextBox, highlight it
        if (startindex >= 0)
        {
            // Set the highlight color as red
            rtb.SelectionColor = Color.Red;
            // Find the end index. End Index = number of characters in textbox
            int endindex = txtSearch.Text.Length;
            // Highlight the search string
            rtb.Select(startindex, endindex);
            // mark the start position after the position of
            // last search string
            start = startindex + endindex;
        }
    }

    public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
    {
        // Unselect the previously searched string
        if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
        {
            rtb.Undo();
        }

        // Set the return value to -1 by default.
        int retVal = -1;

        // A valid starting index should be specified.
        // if indexOfSearchText = -1, the end of search
        if (searchStart >= 0 && indexOfSearchText >=0)
        {
            // A valid ending index
            if (searchEnd > searchStart || searchEnd == -1)
            {
                // Find the position of search string in RichTextBox
                indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
                // Determine whether the text was found in richTextBox1.
                if (indexOfSearchText != -1)
                {
                    // Return the index to the specified search text.
                    retVal = indexOfSearchText;
                }
            }
        }
        return retVal;
    }

// Reset the richtextbox when user changes the search string
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        start = 0;
        indexOfSearchText = 0;
    }

答案 2 :(得分:0)

这将同时显示所有搜索的标准。

使用:1个文本框(用于输入要搜索的文本)和1个按钮(用于运行搜索)。

在文本框中输入搜索条件,然后按搜索按钮。

        // On Search Button Click: RichTextBox ("rtb") will display all the words inside the document
    private void btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            if (rtb.Text != string.Empty)
            {// if the ritchtextbox is not empty; highlight the search criteria
                int index = 0;
                String temp = rtb.Text;
                rtb.Text = "";
                rtb.Text = temp;
                while (index < rtb.Text.LastIndexOf(txt_Search.Text))
                {
                    rtb.Find(txt_Search.Text, index, rtb.TextLength, RichTextBoxFinds.None);
                    rtb.SelectionBackColor = Color.Yellow;
                    index = rtb.Text.IndexOf(txt_Search.Text, index) + 1;
                    rtb.Select();
                }
            }
        }

        catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); }
    }
}

}

答案 3 :(得分:0)

如果你只想匹配整个单词,你可以使用它,请注意这会忽略大小写,而| s \ b意味着复数形式会被突出显示,例如:猫匹配猫而不是蚂蚱:

    public static void HighlightText(RichTextBox myRtb, string word, Color color)
    {
        if (word == string.Empty)
            return;
        var reg = new Regex(@"\b" + word + @"(\b|s\b)",RegexOptions.IgnoreCase);

        foreach (Match match in reg.Matches(myRtb.Text))
        {
            myRtb.Select(match.Index, match.Length);
            myRtb.SelectionColor = color;
        }

        myRtb.SelectionLength = 0;
        myRtb.SelectionColor = Color.Black;
    }

答案 4 :(得分:0)

    private void button3_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != "")
        {
            for (int i = 0; i < richTextBox1.TextLength; i++)
            {
                richTextBox1.Find(textBox1.Text, i, RichTextBoxFinds.None);
                richTextBox1.SelectionBackColor = Color.Red;
            }
        }
        else
        {
            for (int i = 0; i < richTextBox1.TextLength; i++)
            {
                richTextBox1.SelectAll();
                richTextBox1.SelectionBackColor = Color.White;
            }
        }
    }[lets make it!][1]

答案 5 :(得分:0)

之所以这样,是因为所有其他答案都会突出显示该文本,但是在您再次搜索后不会将其更改回去。

使用RichText查找方法查找搜索词的起始索引。

public int FindMyText(string searchText, int searchStart, int searchEnd)
    {
        int returnValue = -1;

        if (searchText.Length > 0 && searchStart >= 0)
        {
            if (searchEnd > searchStart || searchEnd == -1)
            {
                int indexToText = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase);
                if (indexToText >= 0)
                {
                    returnValue = indexToText;
                }
            }
        }

        return returnValue;
    }

使用Button或TextChangeListener并搜索您的单词。

private void button1_Click(object sender, EventArgs e)
{
        // Select the first char in your Richtextbox
        richTextBox1.SelectionStart = 0;

        richTextBox1.SelectionLength = richTextBox1.TextLength;
        // Select until the end
        richTextBox1.SelectionColor = Color.Black;
        // Make the Text Color black

        //Use an Inputfield to add the searching word
        var word = txtSearch.Text;
        //verify the minimum length otherwise it may freeze if you dont have text inside
        if (word.Length > 3)
        {
          int s_start = richTextBox1.SelectionStart, startIndex = 0, index;
          while ((index = FindMyText(word, startIndex, richTextBox1.TextLength)) != -1)
            {
            // goes through all possible found words and color them blue (starting index to end)
                richTextBox1.Select(index, word.Length);
                richTextBox1.SelectionColor = Color.Blue;
                startIndex = index + word.Length;
            }
            // Color everything between in color black to highlight only found words       
            richTextBox1.SelectionStart = startIndex;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionColor = Color.Black;
        }
}

我强烈建议设置最小字长,以避免冻结和增加内存分配。