繁体   English   中英

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

[英]Cannot load an RTF file's text into a RichTextBox?

我正在尝试使用作为资源嵌入的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 \\\\ fan \\ panose 02020603050405020304} Times New Roman;} {\\ f34 \\ fbidi \\ froman \\ fcharset1 \\ fprq2 {* \\ panose 02040503050406030204} Cambria Math;} {\\ f37 \\ fbidi \\ fswiss \\ fcharset204 \\ fprq2 {* \\ panose 020f0502020204030204} \\ f38 \\ fbidi \\ fswiss \\ fcharset204 \\ fprq2 {* \\ panose 020b0604030504040204} Tahoma;}

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

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

我在这里做错了什么?

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

我发现以下csharp-通过http://www.csharp411.com/display-an-rtf-file-thats-ac-embedded-resource/创建的代码

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 )

希望能奏效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM