2010-04-08 51 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; 
相關問題