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

Given the requirements: 1) A User inserts some text in a RichTextBox Control. 2) If the word entered is part of a pre-defined list of words, that word should change color (so, define a relation between a word and a color). 3) When a mouse Click event is generated on a colored word, an event … Read more

How to compare two rich text box contents and highlight the characters that are changed?

First kudos to ArtyomZzz for pointing to the great source of DiffMatchPatch! Here is a piece of code the will paint the changed characters in two RichTextboxes upon a button click. First download the diff-match-patchsource. (!See update below!) From the zip file copy ‘DiffMatchPatch.cs’ and also ‘COPY’ to your project and inlude the cs file … Read more

Richtextbox wpf binding

There is a much easier way! You can easily create an attached DocumentXaml (or DocumentRTF) property which will allow you to bind the RichTextBox‘s document. It is used like this, where Autobiography is a string property in your data model: <TextBox Text=”{Binding FirstName}” /> <TextBox Text=”{Binding LastName}” /> <RichTextBox local:RichTextBoxHelper.DocumentXaml=”{Binding Autobiography}” /> Voila! Fully bindable … Read more

RichTextBox syntax highlighting in real time–Disabling the repaint

It is an oversight in the RichTextBox class. Other controls, like ListBox, support the BeginUpdate and EndUpdate methods to suppress painting. Those methods generate the WM_SETREDRAW message. RTB in fact supports this message, but they forgot to add the methods. Just add them yourself. Project + Add Class, paste the code shown below. Compile and … Read more

Color different parts of a RichTextBox string

Here is an extension method that overloads the AppendText method with a color parameter: public static class RichTextBoxExtensions { public static void AppendText(this RichTextBox box, string text, Color color) { box.SelectionStart = box.TextLength; box.SelectionLength = 0; box.SelectionColor = color; box.AppendText(text); box.SelectionColor = box.ForeColor; } } And this is how you would use it: var userid … Read more