在加载 C# 时将嵌入式资源 .rtf 文件加载到 richtextbox 中

发布于 2024-10-17 21:20:51 字数 577 浏览 7 评论 0原文

好吧,我读到了有关将嵌入式 rtf 加载到富文本框中的内容,并且我尝试在表单加载时执行此操作。该表单不是主表单,它是单击控件时加载的第二个表单。这是加载时表单上的内容:

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);
    }
}

在解决方案资源管理器中,rtf 文件显示为资源,并且构建操作设置为嵌入资源。

当我单击按钮加载表单时,它按预期显示,但没有任何反应。 rtf 的内容似乎没有显示:/

我只能假设我做错了:(

对于这个新手的任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走走停停 2024-10-24 21:20:51

我想通了:

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

需要是:

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

编辑:由于某种原因,这次我无法让上面的代码工作,但我找到了另一个希望这样做的代码要么/要么会对新编码员有一些帮助:)

string rtf = yourAppName.Properties.Resources.yourEmbeddedRTFName;
        yourRichTextBox.Rtf = rtf;

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;
却一份温柔 2024-10-24 21:20:51

我想创建一个名为 Help.rtf 的 rtf 帮助文件,并将其存储为资源,这样就可以在加载帮助表单时加载该文件,而无需在应用程序文件夹周围放置 Help.rtf。

我发现我需要通过菜单的Project->将rtf文件添加到资源中“您的命名空间”属性... 然后导航到“资源”选项卡,然后添加项目等...然后,当我键入“My.Resource”时,智能感知会在其下拉列表中提供 rtf 文件。现在,显示了包括我添加的帮助文件在内的资源列表(没有扩展名,这是正常的)。

简而言之,完成此操作后,接下来的操作就起作用了:

RichTextBox1.rtf = My.Resources.Help

显然,仅仅使用“解决方案资源管理器”拖放、复制甚至添加文件是不够的!

花了三天时间解决这个问题,希望有人觉得它有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文