How can I pass rtf string to richtextbox control

I have a string of rich text characters / tokens that I would like to pass to richtextbox in code.

string rt = @" {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fprq2\fcharset0 Biondi;}}"+ @"{\colortbl ;\red255\green0\blue0;}"+ @"{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20\par"+ @"\cf1\f1 hello\cf0\f0 \ul world\par}"; 

I tried to do this:

  System.IO.MemoryStream strm = new System.IO.MemoryStream(); byte[] b = Encoding.ASCII.GetBytes(rt); strm.BeginRead(b, 0, b.Length, null, null); richTextBox1.LoadFile(strm, RichTextBoxStreamType.RichText); 

it did not work.

can someone give me some advice.

By the way, rich text comes from saving from the keyboard, opening the file with notepad and using text for inside my line

+6
source share
2 answers

A rich text field has an Rtf property. Set this property to your string value. In addition, your line has extra space as the first character. I had to remove this before I see your Hello World.

+11
source share

Extension on gbogumil answer:

 string rt = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fprq2\fcharset0 Biondi;}}"+ @"{\colortbl ;\red255\green0\blue0;}"+ @"{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20\par"+ @"\cf1\f1 hello\cf0\f0 \ul world\par}"; this.richTextBox1.Rtf = rt; 
+2
source share

All Articles