[英]how do I feed an rtf string to a richtextbox control
我有一串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}";
我試過這個:
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。
順便說一下,富文本來自於從wordpad保存,用記事本打開文件並使用in來構建我的字符串
富文本框具有名為Rtf的屬性。 將該屬性設置為您的字符串值。 此外,您的字符串還有一個額外的空格作為第一個字符。 在看到你的Hello World之前我不得不刪除它。
擴展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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.