C # Change the color of a single character in a text box

C # - WPF: how can I change the color of only one character in a text box? Example: Word Hello, color H turns red.

+4
source share
2 answers

You cannot do this with a text box, but you can use richtextbox: WPF RichTextBox Tutorial

var textRange = MyRichTextBox.Selection;
var start = MyRichTextBox.Document.ContentStart;
var startPos = start.GetPositionAtOffset(0);
var endPos = start.GetPositionAtOffset(1);
textRange.Select(startPos, endPos);
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
+4
source

You can use richtexbox as shown below: you can even change the inverse color for a specific character as well

richTextBox1.SelectionStart = characterStartIndex;
richTextBox1.SelectionLength = 1;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectionBackColor = Color.Yellow;
+3
source

All Articles