在 C# 中操作 RichText 的更好方法?

发布于 2024-07-06 08:12:59 字数 468 浏览 8 评论 0原文

我需要创建一些具有标准“格式”(如粗体/斜体、缩进等)的 RichText 并将其复制到剪贴板。 我现在这样做的方式似乎有点不优雅...我正在创建一个 RichTextBox 项目并通过它应用我的格式,如下所示:

RichTextBox rtb = new RichTextBox();
Font boldfont = new Font("Times New Roman", 10, FontStyle.Bold);
rtb.Text = "sometext";
rtb.SelectAll()
rtb.SelectionFont = boldfont;
rtb.SelectionIndent = 12;

必须有更好的方法,但经过几个小时的搜索后我无法想出更好的办法。 有任何想法吗?

编辑: RichTextBox (rtb) 不会在表单上的任何位置显示/绘制。 我只是使用该对象来格式化我的 RichText。

I need to create and copy to the clipboard some RichText with standard "formatting" like bold/italics, indents and the like. The way I'm doing it now seems kind of inelegant... I'm creating a RichTextBox item and applying my formatting through that like so:

RichTextBox rtb = new RichTextBox();
Font boldfont = new Font("Times New Roman", 10, FontStyle.Bold);
rtb.Text = "sometext";
rtb.SelectAll()
rtb.SelectionFont = boldfont;
rtb.SelectionIndent = 12;

There has got to be a better way, but after a few hours of searching I was unable to come up with anything better. Any ideas?

Edit:
The RichTextBox (rtb) is not displayed/drawn anywhere on a form. I'm just using the object to format my RichText.

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

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

发布评论

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

评论(6

染柒℉ 2024-07-13 08:12:59

在执行所有这些操作之前,您可能需要暂停 Richtextbox 的布局,以避免不必要的闪烁。 这是我曾经犯过的常见错误之一,这使得它看起来“不优雅”

You may want to suspend the layout of the richtextbox before you do all of that, to avoid unecessary flicker. That's one of the common mistakes I used to make which made it seem "inelegant"

小忆控 2024-07-13 08:12:59

我认为你的技术是完成你想要做的事情的好方法。 我知道你的意思...感觉有点“肮脏”,因为你将 Winforms 控件用于除预期用途之外的其他用途,但它确实有效。 我已经使用这种技术很多年了。 有兴趣看看其他人是否有可行的选择。

I think your technique is a great way to accomplish what you're looking to do. I know what you mean...it feels kind of "dirty" because you're using a Winforms control for something other than it was intended for, but it just works. I've used this technique for years. Interested to see if anyone else has viable options.

末骤雨初歇 2024-07-13 08:12:59

您可以创建一些实用程序扩展方法以使其更加“优雅”:)

public static RichTextBox Set(this RichTextBox rtb, Font font, string text)
{               
    rtb.Text = text;
    rtb.SelectAll();
    rtb.SelectionFont = font;
    rtb.SelectionIndent = 12;
    return rtb;
}

并像这样调用:

someRtb.Set(yourFont, "The Text").AndThenYouCanAddMoreAndCHainThem();

编辑:我现在看到您甚至没有显示它。 嗯,有趣,抱歉,我在提供非 Rtb 方式方面没有多大帮助。

You could create some utility extension methods to make it more 'elegant' :)

public static RichTextBox Set(this RichTextBox rtb, Font font, string text)
{               
    rtb.Text = text;
    rtb.SelectAll();
    rtb.SelectionFont = font;
    rtb.SelectionIndent = 12;
    return rtb;
}

And call like this:

someRtb.Set(yourFont, "The Text").AndThenYouCanAddMoreAndCHainThem();

Edit: I see now that you are not even displaying it. Hrm, interesting, sorry i wasnt of much help with providing a Non Rtb way.

靑春怀旧 2024-07-13 08:12:59

您还应该在创建后立即暂停 RichTextBox 的布局,并在使用后将其丢弃。

RichTextBox rtb = new RichTextBox();
rtb.SuspendLayout();
//richtext processing goes here...
rtb.Dispose();

并且不要羞于使用 Richtextbox 进行富文本处理。 Microsoft 本身在教程中执行此操作。 :-)

You should also suspend the layout of the richtextbox just after creation and dispose it after use.

RichTextBox rtb = new RichTextBox();
rtb.SuspendLayout();
//richtext processing goes here...
rtb.Dispose();

And don't be shy to use richtextbox for richtext processing. Microsoft itself is doing this here in this tutorial. :-)

北凤男飞 2024-07-13 08:12:59

我知道已经有一段时间了,但是请查看有关将 rtf 转换为 html 的 stackoverflow 帖子。 将您的内容转换为 html,对其进行操作,然后使用 html 显示它或将其转换回 rtf 可能会更容易。

将 Rtf 转换为 HTML

I know it's been a while, but check out this stackoverflow post on converting rtf to html. It would probably be way easier to get your stuff into html, manipulate it, then either display it using html or convert it back to rtf.

Convert Rtf to HTML

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