0

It changes the color of a selected text, but the selected text appears when typing in the object. How can I solve this?

My function:

    private void special_words_colors()
            {
                string s_words = @"\b(for|while|if)\b";
                MatchCollection eleman_eslesmeler = Regex.Matches(input:richTextBox1.Text,
pattern: s_words);
    
                foreach(Match m in s_words)
                {
                    richTextBox1.SelectionStart = m.Index;
                    richTextBox1.SelectionLength = m.Length;
                    richTextBox1.SelectionColor = Color.Orange;
                   
                }
                richTextBox1.Focus();
    
            }

My textChanged Event :

 private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            special_words_colors();
} 

it is working but while is writing seeing like this :

enter image description here

urdurak
  • 28
  • 1
  • 4
  • Does this answer your question? [Color specific words in RichtextBox](https://stackoverflow.com/questions/21980554/color-specific-words-in-richtextbox) – 41686d6564 stands w. Palestine May 05 '22 at 00:00
  • [How to color different words with different colors in a RichTextBox while a user is writing and raise an event when that colored text is clicked](https://stackoverflow.com/a/48358496/7444103) – Jimi May 05 '22 at 00:02
  • Instead of `richTextBox1.Focus();`, set the `SelectionStart` and `SelectionColor` properties to their original values. See the linked post for an example. – 41686d6564 stands w. Palestine May 05 '22 at 00:02
  • Or you may want to use [Monaco editor in WinForms application](https://stackoverflow.com/a/71885536/3110834). – Reza Aghaei May 05 '22 at 00:23
  • @41686d6564standsw.Palestine Thank you very much. İt's worked. But sometimes show selected text. I hope i can solve it. – urdurak May 05 '22 at 00:24

0 Answers0