Click here to Skip to main content
15,881,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to color some line which are going to print in recitextbox like 100/2 claim not found, but I want to select only 100/2 and then all other line will print in that same color which I am giving to 100/2 how to do that, this is below code that I have done till now what can I change in it so I can print whole line when its select only 100/2

What I have tried:

private void CheckKeyword(string word, Color color, int startIndex)
      {

          if(this.richTextBox1.Text.Contains(word))
          {
              int index = -1;
              int selectStart = this.richTextBox1.SelectionStart;
              while ((index = this.richTextBox1.Text.IndexOf(word,(index + 1))) != -1)
              {

                  this.richTextBox1.Select((index ), word.Length);
                  this.richTextBox1.SelectionColor = color;
                  this.richTextBox1.Select(selectStart, 0);
                  this.richTextBox1.SelectionColor = Color.Black;
              }

          }
      }

      public void richTextBox1_TextChanged(object sender, EventArgs e)
      {
          this.CheckKeyword("106/2", Color.Purple, 0);
          this.CheckKeyword("Code: 100/4", Color.Red, 0);

      }
Posted
Updated 17-Feb-22 1:10am

1 solution

You are only adding the colour to the actual word that is found in the text box. You need to set the length field based on the index position where the cursor is. Change the first line of the while loop in the CheckKeyword method to the following:
C#
this.richTextBox1.Select((index), selectStart - index - 1);

Note that as soon as you select any further characters in the line it will change the colouring, so you may need to keep a flag or counter to check how many times it is being changed.
 
Share this answer
 
v2
Comments
Maciej Los 17-Feb-22 14:25pm    
5ed!

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