2011-11-30 97 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

相關問題