【问题标题】:Cannot load an RTF file's text into a RichTextBox?无法将 RTF 文件的文本加载到 RichTextBox 中?
【发布时间】:2011-11-30 22:10:57
【问题描述】:

我正在尝试使用作为资源嵌入的 RTF 文件加载 RichTextBox:

Private Sub Button1_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Form2.Show()
    Form2.RichTextBox1.Text = Global.MyApp.My.Resources.RTFFile

但是当我这样做时,RTF 的标记不会被解释,RTB 会显示以下内容:

{\rtf1\adeflang1025\ansi\ansicpg1251\uc1\adeff0\deff0\stshfdbch37\stshfloch37\stshfhich37\stshfbi0\deflang1049\deflangfe1049\themelang1049\themelangfe0\themelangcs0{\fonttbl{\f0\fbidi \froman\fcharset204\ {*\panose 02020603050405020304}Times New Roman;}{\f34\fbidi \froman\fcharset1\fprq2{*\panose 02040503050406030204}Cambria Math;} {\f37\fbidi \fswiss\fcharset204\fprq2{*\panose 020f0502020204030204}Calibri;}{\f38\fbidi \fswiss\fcharset204\fprq2{*\panose 020b0604030504040204}Tahoma;}

当我从磁盘加载 RTF 文件时它可以工作:

Form2.Show()
Form2.RichTextBox1.LoadFile("C:\1.rtf")

我在这里做错了什么?

【问题讨论】:

  • 请检查我的编辑。您将看到如何格式化代码和引用文本。编辑器也有广泛的帮助,只需点击最右侧的?
  • 简单错误:设置 Rtf 属性,而不是 Text 属性。

标签: vb.net


【解决方案1】:

您必须像文件一样通过加载方法转换富文本。这可以通过流的重载来完成

我通过http://www.csharp411.com/display-an-rtf-file-thats-a-c-embedded-resource/找到了以下csharp-code

Assembly asm = Assembly.GetExecutingAssembly();
Stream stream = asm.GetManifestResourceStream( "MyNamespace.FileName.rtf" );

RichTextBox rt = new RichTextBox();
rt.LoadFile( stream, RichTextBoxStreamType.RichText );

这必须被翻译成 VB.NET.. 类似这样的东西。 记得包含 system.reflection

dim asm as Assembly = Assembly.GetExecutingAssembly()
dim stream as Stream = asm.GetManifestResourceStream( "MyNamespace._1.rtf" )
Form2.RichTextBox1.LoadFile(stream,  RichTextBoxStreamType.RichText )

希望有效果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 2011-07-16
    相关资源
    最近更新 更多