Hi all How can I change the color of a char in a string using C#? for ex. MIND, make D to be red and the rest remain black.
I'm using a WINFORMS and I try to display it in a textbox, I can Use richtextbox as well.
Hi all How can I change the color of a char in a string using C#? for ex. MIND, make D to be red and the rest remain black.
I'm using a WINFORMS and I try to display it in a textbox, I can Use richtextbox as well.
You do not. Strings have no color. PRESENTATION of strings may have a color, but that is not something you determine in the string.
You can do that with a RichTextBox at least.
// Save selection
var oldStart = richTextBox1.SelectionStart;
var oldLength = richTextBox1.SelectionLength;
// Select the text to change
richTextBox1.Select(richTextBox1.TextLength - 1, 1);
// Change color
richTextBox1.SelectionColor = Color.Red;
// Restore selection
richTextBox1.Select(oldStart, oldLength);
Using the RichTextBox control, check out the tutorial on Syntax Highlighting presented here: