Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using the following code to search for text in a richtext box:

C#
richTextBox1.Find(textBox1.Text, index, richTextBox1.TextLength, RichTextBoxFinds.None);


if I input:
textbox1 : int

the richtextbox1 content is highlighted like below:
richtextbox1: You are intelligent but intimidating


but I want the highlighting to be as follows for the same input in textbox1 :
You are intelligent but intimidating.


Please advice.. Thank you..
Posted
Updated 15-Sep-14 20:39pm
v3

C#
private void button1_Click(object sender, EventArgs e)
       {
           selectWords(richTextBox1.Text, textBox1.Text);
       }

       private void selectWords(string fullText, string searchText)
       {
           string[] words = fullText.Split(' ');
           foreach (var word in words)
           {
               if (word.Contains(searchText))
               {
                   richTextBox1.Find(word, 0, richTextBox1.TextLength, RichTextBoxFinds.None);
                   richTextBox1.SelectionBackColor = Color.Green;
               }
           }
       }
 
Share this answer
 
Comments
Tejas Shastri 15-Sep-14 12:34pm    
this code is now not highlighting the actual searchtext when it appears in the fulltext.. its not an issue but i'm not able to figure out the reason for this. i want it to highlight the actual searchtext if it appears in the fulltext.
Hi,
I dont know the exact reason why solution1 is not highlighting actual searchtext. May be using every time zero(0) as starting index in find method. Hence i modified as below.

C#
string[] words = fullText.Split(' ');
           int index = 0;
           foreach (var word in words)
           {
               if (word.Contains(searchText))
               {
                   index = richTextBox1.Find(word, index, richTextBox1.TextLength, RichTextBoxFinds.None);
                   richTextBox1.SelectionBackColor = Color.Green;
                   index++;
               }
           }
 
Share this answer
 
You can try regular expression
This expression will only work for lower case letters.
C#
textBox1.Text = @"int\w*";


This is an assumed function in your Form
C#
private void buttonHighLight_Click(object sender, EventArgs e)
{
   // If the input contains the regular expression part
   HighLightWords(textBox1.Text);
   // or if you don't want the user to deal with regular expressions, you can add this part in code.
   HighLightWords(String.Format(@"{0}\w*", textBox1.Text);
}


This will highlight any word that matches the input expression.
C#
private void HighLightWords(string wordExpression)
{
    Regex regex = new Regex(wordExpression, RegexOptions.CultureInvariant | RegexOptions.Singleline);
    foreach (Match m in regex.Matches(richTextBox1.Text))
    {
        richTextBox1.SelectionStart = m.Index;
        richTextBox1.SelectionLength = m.Length;
        richTextBox1.SelectionBackColor = Color.Yellow;
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900