Hello.

I wanted to implement such a function so that in the handler for clicking on a button, a certain text was displayed in richTextBox, while one word from this text was painted in a certain color, and the rest went in the usual color.

private void PersonShow_Click(object sender, EventArgs e) { string result = classArray[0].Show(); rText.Text += "Person:" + result + "\n"; // rText.SelectionLength = 7; // rText.SelectionColor = Color.DarkCyan; } 

2 lines I commented out. I wanted Person to be displayed in the color DarkCyan, and the rest black. If you click on the button once - everything is done, as I planned, if you click twice, the subsequent added text and so on are already being painted.

PS: Is there a function in C # that could highlight a specific word in richTextBoxe, or is it just the next word?

    2 answers 2

     string mystring=@"my first string"; if (richTextBox1.Find(mystring)>0) { int my1stPosition = richTextBox1.Find(mystring); richTextBox1.SelectionStart = my1stPosition; richTextBox1.SelectionLength = mystring.Length; richTextBox1.SelectionColor = Color.DarkCyan; } 

    If there are several such words, turn it all into foreach.

    • one
      Probably the same while, not foreach :) - Modus

    You can generate rtf-text by inserting formatting there. You can spy on the property Rtf.

    • Hmm ... Thanks, of course, but if I still knew how to generate it and where to look ... - Programmer