Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I feed an rtf string to a richtextbox control

Tags:

c#

.net-2.0

I have a string of richtext characters/tokens that I would like to feed to a 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 have attempted 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 didn't work.

can anyone give me a few sugestions.

BTW the rich text comes from saving from wordpad, opening the file with notepad and using the text with in to build my string

like image 709
fishhead Avatar asked Apr 08 '10 17:04

fishhead


2 Answers

Rich textbox has a property named Rtf. Set that property to your string value. Also, your string has an extra space as the first character. I had to remove that before I saw your Hello World.

like image 95
Greg Bogumil Avatar answered Nov 06 '22 14:11

Greg Bogumil


Expanding on gbogumil's 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;
like image 28
Inisheer Avatar answered Nov 06 '22 16:11

Inisheer