שתף באמצעות


Reading an RTF Resource File into a Rich Text Box

Question

Friday, October 13, 2017 12:45 PM

Hi All,

My applications documentation files are in an RTF format, that I created in MS Word. I want to avoid the need for an installer to put these files into a Program Data folder when I deploy the application, so I have put them in my project's resources. First time using resources, and I'm a bit confused as how to read these files into a rich text box.

Been looking at this documentation, but nothing seems to fit my needs. https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/objects/my-resources-object

If anybody can give a short example of loading an RTF resource file into a Rich Text Box, it would be greatly appreciated.

Thanks

DW

All replies (7)

Friday, October 13, 2017 6:09 PM ✅Answered

Also having a problem with this.

{} NameSpace ProjectName.My.Resources

'Overview' is not a member of 'ProjectName.My.Resources'

rTxt_Help.Rtf = My.Resources.Overview.rtf

 You do not need to use the extension (.rtf) in that line of code.  The error you are describing indicates to me that you have not actually added the file(s) to the application's resources.  Or that you have not added a file with the name 'Overview' to the resources.

 To add them to the application's resources,   find your (.rtf) files in the windows explorer,  right click and Copy them.  Now in the VS editor,  go to the menu and click (Project) and select (Your Project Name Properties...).  When the properties open,  select the (Resources) tab on the left.  Then on the right,  right click and paste the files in.

 If added correctly,  then this should work...

rTxt_Help.Rtf = My.Resources.Overview

 

 I should also add that this is not a good choice if you are adding a large amount of files or your files are large.

If you say it can`t be done then i`ll try it


Friday, October 13, 2017 1:56 PM

Hi

Here is some code that may help. This example has 2 rtf files in My.Resources and simply cycles through the with each Button1 clcik.

Form has RichTextBox1, Button1

Option Strict On
Option Explicit On
Public Class form1
    Dim lst As New List(Of String)
    Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' My.Resources holds two files
        ' could be any number.
        With lst
            .Add(My.Resources.Testing2)
            .Add(My.Resources.Testing3)
        End With
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static c As Integer = 0
        LoadToRTB(RichTextBox1, lst(c))
        c += 1
        If c > lst.Count - 1 Then c = 0
    End Sub
    Sub LoadToRTB(rtb As RichTextBox, s As String)
        Using memstream As New IO.MemoryStream()
            Dim buff As Byte() = System.Text.Encoding.UTF8.GetBytes(s)
            memstream.Write(buff, 0, buff.Length)
            memstream.Seek(0, IO.SeekOrigin.Begin)
            rtb.LoadFile(memstream, RichTextBoxStreamType.RichText)
        End Using
    End Sub
End Class

Regards Les, Livingston, Scotland


Friday, October 13, 2017 4:12 PM

 When you add a rich text file to the application's resources,  it stores the file as a string of the rtf.  All you need to do is set the RichTextBox.Rtf Property to the rtf string you have added to the resources.  For example,  i added an (.rtf) file in my resources and used the following to load/display it in a RichTextBox.

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RichTextBox1.Rtf = My.Resources.My_Rtf_File
    End Sub

End Class

 

If you say it can`t be done then i`ll try it


Friday, October 13, 2017 4:39 PM

Thank you for your quick response.

I am having a problem with this code adapted as below:

What is happening is buff.Length = either 12 or 25, which is the number of characters in hFilename, not the number of bytes actually in the file. Perhaps I am still not properly defining the resource filename? Frm_Main.rTxt_Help is the RichTextBox.

' I've defined the filename with either of these, obviously using only one and commenting out the other

hFilename = "Overview.rtf"
hFilename = "My.Resources.Overview.rtf"

Using memstream As New IO.MemoryStream()
    Dim buff As Byte() = System.Text.Encoding.UTF8.GetBytes(hFilename)
    memstream.Write(buff, 0, buff.Length)
    memstream.Seek(0, IO.SeekOrigin.Begin)
    Frm_Main.rTxt_Help.LoadFile(memstream,RichTextBoxStreamType.RichText)
End Using

Friday, October 13, 2017 4:50 PM

 Forgot to mention,  if you need to or want to save the rtf files from the resources to the hard drive,  then you can just save it with the File.WriteAllText method.

IO.File.WriteAllText("C:\Testfolder\MyRtfFile.rtf", My.Resources.My_Rtf_File)

If you say it can`t be done then i`ll try it


Friday, October 13, 2017 5:26 PM

Also having a problem with this.

{} NameSpace ProjectName.My.Resources

'Overview' is not a member of 'ProjectName.My.Resources'

rTxt_Help.Rtf = My.Resources.Overview.rtf

Friday, October 13, 2017 6:00 PM

Thank you for your quick response.

I am having a problem with this code adapted as below:

What is happening is buff.Length = either 12 or 25, which is the number of characters in hFilename, not the number of bytes actually in the file. Perhaps I am still not properly defining the resource filename? Frm_Main.rTxt_Help is the RichTextBox.

' I've defined the filename with either of these, obviously using only one and commenting out the other

hFilename = "Overview.rtf"
hFilename = "My.Resources.Overview.rtf"

Using memstream As New IO.MemoryStream()
    Dim buff As Byte() = System.Text.Encoding.UTF8.GetBytes(hFilename)
    memstream.Write(buff, 0, buff.Length)
    memstream.Seek(0, IO.SeekOrigin.Begin)
    Frm_Main.rTxt_Help.LoadFile(memstream,RichTextBoxStreamType.RichText)
End Using

Hi

When I added the rtf files to My.Resources, the names are without the extension. So, Overview.rtf becomes Overview Try using that.

The Resources are not files. I thought you wanted embeded resources instead of files (files would actually be easier to deal with)

Regards Les, Livingston, Scotland