2011-11-30 61 views
0

我试图使用嵌入作为资源的RTF文件来加载一个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 \ fbidi \费勒曼\ fcharset204 \ fprq2 {* \ panose 02020603050405020304} Times New Roman;} {\ f34 \ fbidi \ froman \ fcharset1 \ fprq2 {* \ panose 02040503050406030204} Cambria Math;} {\ f37 \ fbidi \ fswiss \ fcharset204 \ fprq2 {* \潘020f0502020204030204}宋体;} {\ F38 \ fbidi \ fswiss \ fcharset204 \ fprq2 {* \潘020b0604030504040204}宋体;}

它的工作原理,当我加载RTF从磁盘文件:

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

我在做什么错在这里?

+0

请仔细阅读我的编辑。您将看到如何格式化代码和引用文本。编辑也有广泛的帮助,只需点击最右边的'?'即可。 – Will

+0

简单的错误:设置Rtf属性,而不是Text属性。 –

回答

0

你必须通过加载方法来转换富文本,就像它是文件时一样。代码通过http://www.csharp411.com/display-an-rtf-file-thats-a-c-embedded-resource/

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

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

这有翻译成VB.NET ..这样的事情 - 这可以通过与流

我发现下面的CSHARP过载来完成。 记得要包括的System.Reflection

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

希望工程

+0

如何使用它在vb.net – user1072795

+0

我还没有尝试过上面的代码 - 但我想它会工作 – Burrhus

相关问题