无法将RTF文件的文本加载到RichTextBox中?

时间:2011-11-30 07:05:19

标签: vb.net

我正在尝试使用嵌入资源的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 \ fprq2 {* \ 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")

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

您必须像使用文件一样通过加载方法转换富文本。这可以通过流

的重载来完成

我通过http://www.csharp411.com/display-an-rtf-file-thats-a-c-embedded-resource/

找到了以下csharp代码
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 )

希望它有效