I had the same problem. In the end, I ignored all the methods they gave you and manipulated the RTF data. Also, the reason your second block of code doesn't work is because RTF applies formatting as it appears, so if you call a function (or property in this case) to change the highlight color, it only applies to to the currently selected block. Any changes made to the selection after this call become invulnerable.
You can play with RGB values, or here is a great source on how to do different things in an RTF control. Paste this function into your code and see how it works. I use it to provide real-time syntax highlighting for SQL code.
public void HighlightText(int offset, int length) { String sText = richTextBox.Text.Trim(); sText = sText.Insert(offset + length - 1, @" \highlight0"); sText = sText.Insert(offset, @" \highlight1"); String s = @"{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}} {\colortbl ;\red255\green255\blue0;}\viewkind4\uc1\pard"; s += sText; s += @"\par}"; richTextBox.Rtf = s; }
MrWuf source share