如何将rtf字符串提供给richtextbox控件

fis*_*ead 6 c# .net-2.0

我有一串richtext字符/标记,我想在代码中提供给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}";
Run Code Online (Sandbox Code Playgroud)

我试过这个:

      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);
Run Code Online (Sandbox Code Playgroud)

它不起作用.

任何人都可以给我一些sugestions.

顺便说一下,富文本来自于从wordpad保存,用记事本打开文件,并使用in来构建我的字符串

Gre*_*mil 12

富文本框具有名为Rtf的属性.将该属性设置为您的字符串值.此外,您的字符串还有一个额外的空格作为第一个字符.在看到你的Hello World之前我不得不删除它.


Ini*_*eer 5

扩展 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;
Run Code Online (Sandbox Code Playgroud)