vijay mani

vijay mani

  • NA
  • 7
  • 3.9k

Syntax highlighting in richtextbox

Feb 5 2015 12:42 AM
How do i highlight the CSS keywords in richtextbox using c#?
I had css parser to split the selector, property and value and used following code for this purpose,
        CssParser parser = new CssParser();
            var rules = parser.ParseAll(RichEditor.Text);
            StringBuilder selectorBuilder = new StringBuilder();
            StringBuilder propertyBuilder = new StringBuilder();
            StringBuilder ValueBuilder = new StringBuilder();
            Regex selectorRegex, mediaRegex, propertyRegex, valueRegex;
            int cursorPosition = RichEditor.SelectionStart;
            int lineIndex = RichEditor.GetLineFromCharIndex(cursorPosition);

            foreach (var rule in rules)
            {

                foreach (var selector in rule.Selectors)
                {
                  
                    selectorBuilder.Append(selector.ToString() + "|");
                }
                if (rule.Media != null)
                {
                    mediaBuilder.Append(rule.Media.ToString() + "|");
                }
                foreach (var declaration in rule.Declarations)
                {
                    propertyBuilder.Append(declaration.Property.ToString() + "|");
                    ValueBuilder.Append(declaration.Value.ToString() + "|");
                }

            }

            selectorRegex = new Regex(selectorBuilder.ToString());
            mediaRegex = new Regex(mediaBuilder.ToString());
            propertyRegex = new Regex(propertyBuilder.ToString());
            valueRegex = new Regex(ValueBuilder.ToString());

            // Select all and set to black so that it's 'clean'
            RichEditor.SelectAll();
            RichEditor.SelectionColor = System.Drawing.Color.Black;

            // Then unselect and scroll to the end of the file
            RichEditor.ScrollToCaret();
            RichEditor.Select(RichEditor.Text.Length, 1);

            // Start applying the highlighting... Set a value to selPos
            int selPos = RichEditor.SelectionStart;

            foreach (Match keyWordMatch in selectorRegex.Matches(RichEditor.Text))
            {
              
                RichEditor.Select(keyWordMatch.Index, keyWordMatch.Length);
             
                RichEditor.SelectionColor = System.Drawing.Color.Brown;
              
                RichEditor.SelectionStart = selPos;
                
            }


            selPos = RichEditor.SelectionStart;

            foreach (Match keyWordMatch in mediaRegex.Matches(RichEditor.Text))
            {
              
                RichEditor.Select(keyWordMatch.Index, keyWordMatch.Length);
              
                RichEditor.SelectionColor = System.Drawing.Color.Red;
              
                RichEditor.SelectionStart = selPos;
               
            }

            selPos = RichEditor.SelectionStart;

            foreach (Match keyWordMatch in propertyRegex.Matches(RichEditor.Text))
            {
               
                RichEditor.Select(keyWordMatch.Index, keyWordMatch.Length);
          
                RichEditor.SelectionColor = System.Drawing.Color.Red;
               
                RichEditor.SelectionStart = selPos;
               
            }

            selPos = RichEditor.SelectionStart;

            foreach (Match keyWordMatch in valueRegex.Matches(RichEditor.Text))
            {
         
                RichEditor.Select(keyWordMatch.Index, keyWordMatch.Length);
       
                RichEditor.SelectionColor = System.Drawing.Color.Blue;
             
                RichEditor.SelectionStart = selPos;
              
                RichEditor.SelectionColor = System.Drawing.Color.Black;
            } 
 
But every time the text in richtextbox is flickering.
Is there any solution for this purpose?
 

Answers (2)