2010-04-08 38 views
6

我有一个富文本字符/标记字符串,我想将它提供给代码中的richtextbox。如何将一个RTF字符串提供给一个richtextbox控件

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}"; 

我已经尝试这样的:

 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); 

没有奏效。

任何人都可以给我几个sugestions。

BTW富文本来自从写字板保存,用记事本打开该文件,并使用该文本在建立我的字符串

回答

11

格式文本框有一个名为RTF属性。将该属性设置为您的字符串值。另外,你的字符串还有一个额外的空间作为第一个字符。在我看到你的Hello World之前,我必须删除它。

+2

+1注意到空间 – 2010-04-08 17:51:17

2

上gbogumil的回答扩展:

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; 
相关问题