Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading embedded resource .rtf file into richtextbox on load C#

Ok so I read about loading an embedded rtf into a rich text box and I am trying to do it when the form loads. The form is not the main form, it is a second form that loads when a control is clicked. This is what I have on the form when it loads:

private void Credits_Load(object sender, EventArgs e)
{
    Assembly creditAssm = Assembly.GetExecutingAssembly();
    using (Stream creditStream =
    creditAssm.GetManifestResourceStream("YDisplayView.credits.rtf"))
    {
        creditsRichTextBox.LoadFile(creditStream, RichTextBoxStreamType.RichText);
    }
}

In the solution explorer the rtf file shows as a resource and the build action is set to embedded resource.

when I click the button to load the form, it shows as expected, but nothing happens. The contents of the rtf doesn't seem to show :/

I can only assume I am doing it wrong :(

Any help for this newbie would be appreciated.

like image 855
BrandNewDev Avatar asked Feb 18 '11 06:02

BrandNewDev


2 Answers

I figured it out:

creditAssm.GetManifestResourceStream("YDisplayView.credits.rtf"))

needed to be:

creditAssm.GetManifestResourceStream("YDisplayView.Resources.credits.rtf"))

Edit: For some reason this time around I can't get the above code to work but I found another one that did so hopefully either/or will be of some help to new coders out there :)

string rtf = yourAppName.Properties.Resources.yourEmbeddedRTFName;
        yourRichTextBox.Rtf = rtf;
like image 97
BrandNewDev Avatar answered Nov 18 '22 08:11

BrandNewDev


I wanted to create an rtf help file named Help.rtf and have it stored as a resource and so it could be loaded when a Help form was loaded without having a Help.rtf hanging around the application folders.

I found I needed to add the rtf file to the resources through the menu's Project-> 'Your name space' Properties... then navigate to Resources tab, then Add Item etc etc... before the intellisense would offer the rtf file in it's drop-down list when I typed 'My.Resource.' Now, the list of resources including the Help file I added shows up (without extension, which is normal).

In short, once this was done, then the following worked:

RichTextBox1.rtf = My.Resources.Help

Apparently it wasn't enough just to Drag&Drop, Copy or even add the file using the 'Solution Explorer'!

Spent 3 days on this problem so I hope someone finds it usefull.

like image 4
Fellow Coder Avatar answered Nov 18 '22 06:11

Fellow Coder