Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Change color of one character in a text box

Tags:

c#

.net

wpf

C# - WPF : how can I change the color of just one character in a text box ? example : Word Hello, Color of H becomes Red

like image 622
daniyalahmad Avatar asked Feb 14 '23 14:02

daniyalahmad


1 Answers

You can not do this with a textbox, but you can use a 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));
like image 198
user1567896 Avatar answered Feb 19 '23 19:02

user1567896