0

I have read a number of articles about formatting text in a RichTextBox as the user types by selecting text based on a search and then setting the SelectionColour property; I don't like this as the flashy behaviour of the selection doesn't look very good.

With that in mind, I am instead using a procedure to 'build' the raw RTF text which defines the formatting required based on the text and then just sets the Rtf property of the RichTextBox.

However, what I can't understand is that, after I've created the raw RTF string, when this is passed to the Rtf property of the RichTextBox, it's getting overridden by something completely different.

This is the raw RTF string which I've created with the procedure:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\colortbl;\red0\green0\blue0;
\red65\green105\blue225;\red255\green0\blue0;\red255\green192\blue203;
\red128\green128\blue128;\red255\green165\blue0;\red34\green139\blue34;}
{\fonttbl\n{\f0\fswiss\fcharset1252 Courier New;}\n}{\cf2select \cf5left}}

When I set the Rtf property of the RichTextBox to this string, what it actually sets it to is:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl
{\f0\fswiss\fcharset0 Courier New;}}\r\n{\colortbl;\red65\green0\blue0;\red0\green0\blue0;}
\r\n\viewkind4\uc1\pard\cf1\f0\fs24 se\\cf5\cf2 cf1lect left\par\r\n}\r\n

What is even more confusing to me is that if I set the Rtf property of the RichTextBox in the form constructor rather than in this procedure, it does set it to the passed value exactly and I get the text formatting that I want.

What I am trying to understand is why the Rtf property is not being set to what I pass it and seems to be getting set to something completely different.

Your_Unequal
  • 246
  • 1
  • 5
  • 17
  • Instead of trying to change rtf, use [this technique](http://stackoverflow.com/a/3282911/3110834) to disable flicker when updating `SelectionBackColor`. – Reza Aghaei Nov 10 '16 at 01:21
  • Thanks for the pointer; this seems to be working quite well initially; I have not stress tested it yet but it looks very promising. If you'd like to post an answer for this I'd be happy to up-vote it; although I think I may want to keep it open as I would still like to really understand (albeit more for academic purposes at this point) why the Rtf property behaves in this way :-) – Your_Unequal Nov 10 '16 at 11:07
  • You're welcome and thanks for the offer. Since the linked post is a complete answer, I'm not goring to post an answer based on that. Instead, when you find an answer useful, you can vote for it :) – Reza Aghaei Nov 10 '16 at 11:22

0 Answers0