How can I make sure that it will highlight the specific chars only if typing them once each time in the TextChanged event?

sharon glipman 441 Reputation points
2021-08-27T19:16:29.183+00:00
private void richTextBox2_TextChanged(object sender, EventArgs e)  
        {  
            if ((richTextBox2.Text != "" && textBoxRootDirectory.Text != "" &&  
                Directory.Exists(textBoxRootDirectory.Text)) || richTextBox2.Text != "")  
            {  
                startButton.Enabled = true;  
                Properties.Settings.Default["Setting2"] = richTextBox2.Text;  
                Properties.Settings.Default.Save();  
            }  
            else  
            {  
                startButton.Enabled = false;  
            }  
  
            if (IsColouring) return;  
  
            IsColouring = true;  
  
            try  
            {  
                if (richTextBox2.Text.Length > 1 && !richTextBox2.Text.Substring(0,2).StartsWith(",,"))   
                {  
                    Highlight(richTextBox2);  
                }  
            }  
            finally  
            {  
                IsColouring = false;  
            }  
        }  

And

void Highlight(RichTextBox richTextBox)  
        {  
            const int WM_SETREDRAW = 0x000B;  
  
            SendMessage(richTextBox.Handle, WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero);  
  
            var sel_start = richTextBox.SelectionStart;  
            var sel_len = richTextBox.SelectionLength;  
  
            richTextBox.SelectAll();  
            richTextBox.SelectionColor = Color.Black;  
  
            int p = 0;  
            while ((p = richTextBox.Find(",,", p, RichTextBoxFinds.None)) >= 0)  
            {  
                richTextBox.SelectionStart = p;  
                richTextBox.SelectionLength = 2;  
                richTextBox.SelectionColor = Color.Red;  
  
                p += 1;  
            }  
  
            richTextBox.SelectionStart = sel_start;  
            richTextBox.SelectionLength = sel_len;  
  
            SendMessage(richTextBox.Handle, WM_SETREDRAW, new IntPtr(1), IntPtr.Zero);  
            richTextBox.Invalidate();  
        }  
  
        [DllImport("user32")]  
        private extern static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);  

The problem is in case I type for example in the richTExtBox2 :

Hello World,,,,,,,,

It will color in red all the ,, chars but I want that it will color the first two ,, in red and if I keep typing ,, after it than the next ,, will be in black. only if there is a space between the ,,,,, or letters or other chars color the ,, in red.

The goal is to color each time only two ,, in red and if there is more than two ,, in a row than color only the first two in red the rest in black.

,,,,,,
^
This two in red the rest should be in black

,,hello,,hi,,,,,,
^ ^ ^ ^ ^
red red black

I will add a screenshot image to explain better :

The chars ,, are for separate.

So in this screenshot image : The chars in the end ,,,,,,,,,,,, they are all red but they should not be red but black.
Because only if I type two ,, and than a letter or something other chars it should keep the two in red and the rest the next in black.
But if I didn't type any letters or other chars after two ,, than the two ,, should become black with the rest ,, so all this ,,,,,,,,,,,, in the end should be black.

The separate rule is to separate only between words letters or chars that are not two or more ,, in a row.

So Hello,,World make the two ,, in red but Hellow,,World,,,,,,,,,, make all the ,,,,,, in the end after the World in black because there is nothing to separate it's all ,,,,,,,,,,

127261-mysep1.jpg

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,862 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,467 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 113.7K Reputation points
    2021-08-27T19:56:08.123+00:00

    Check this adjustment:

    void Highlight( RichTextBox richTextBox )
    {
       const int WM_SETREDRAW = 0x000B;
    
       SendMessage( richTextBox.Handle, WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero );
    
       var sel_start = richTextBox.SelectionStart;
       var sel_len = richTextBox.SelectionLength;
    
       richTextBox.SelectAll( );
       richTextBox.SelectionColor = Color.Black;
    
       for( int y = 0; y < richTextBox.Lines.Length; y++ )
       {
          string s = richTextBox.Lines[y];
          int x = richTextBox.GetFirstCharIndexFromLine( y );
    
          foreach( Match m in Regex.Matches( s, @"(?<!^)(?<=[^,])(?<sep>,,),*?[^,]" ) )
          {
             richTextBox.SelectionStart = x + m.Groups["sep"].Index;
             richTextBox.SelectionLength = 2;
    
             richTextBox.SelectionColor = Color.Red;
          }
       }
    
       richTextBox.SelectionStart = sel_start;
       richTextBox.SelectionLength = sel_len;
    
       SendMessage( richTextBox.Handle, WM_SETREDRAW, new IntPtr( 1 ), IntPtr.Zero );
       richTextBox.Invalidate( );
    }
    

0 additional answers

Sort by: Most helpful