【问题标题】:how do I feed an rtf string to a richtextbox control如何将 rtf 字符串提供给 Richtextbox 控件
【发布时间】:2010-04-09 01:17:02
【问题描述】:

我有一串富文本字符/令牌,我想将其提供给代码中的富文本框。

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

它没有工作。

谁能给我一些建议。

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

【问题讨论】:

    标签: c# .net-2.0


    【解决方案1】:

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

    【讨论】:

      【解决方案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;
      

      【讨论】:

        猜你喜欢
        • 2019-06-21
        • 1970-01-01
        • 2010-11-24
        • 1970-01-01
        • 2011-03-19
        • 1970-01-01
        • 2011-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多