有没有办法克服 RichTextBox 的局限性?

发布于 2024-09-06 04:46:34 字数 538 浏览 0 评论 0原文

我正在使用 WinForms 和 RichTextBox 控件开发一个应用程序。此控件允许使用属性更改所选文本的字体来对 RTF 格式进行不同的更改。我有一个按钮可以在文本上切换粗体:

richTextBoxEditor.SelectionFont = new Font(richTextBoxEditor.SelectionFont,
    richTextBoxEditor.SelectionFont.Style ^ FontStyle.Bold);

我的其他按钮(斜体、下划线)具有相同的代码,但 FontStyle.Bold 部分除外,该部分已更改为适当的格式。

现在的问题是,如果我写“Hello World”并将“Hello”更改为斜体,然后我尝试将整个“Hello World”更改为粗体,“Hello”就会失去斜体。我理解它为什么这样做(因为整个选择的枚举是空的,所以当我切换粗体时,斜体标志保持为 0),但我正在尝试找到一种方法来克服这个问题。我想不出除了逐个角色和单独改变风格之外还有什么。

你们还有更好的主意吗?

谢谢。

I am developing an app using WinForms and the RichTextBox control. This control allows different changes to the RTF formatting using a property to change the font of the selected text. I have a button to toggle bold on the text:

richTextBoxEditor.SelectionFont = new Font(richTextBoxEditor.SelectionFont,
    richTextBoxEditor.SelectionFont.Style ^ FontStyle.Bold);

My other buttons (italic, underline) have identical code except for the FontStyle.Bold part which is changed for the appropriate formatting.

Now the problem with this is, if I write "Hello World" and change the "Hello" to say italic, and then I try to change the whole "Hello World" to bold, "Hello" loses its italic. I understand why it does this (because the enum for the whole selection is empty so when I toggle bold, the italic flag remains to 0), but I am trying to find a way to overcome this. I can't think of anything short of going character by character and changing style individually.

Would you guys have any better idea?

Thanks.

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

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

发布评论

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

评论(1

南风起 2024-09-13 04:46:34

听起来逐个字符是你最安全的选择。除非您正在创建一个成熟的文字处理器,否则这似乎不是一个重大问题。

另一种选择是根据当前样式分解选择。因此,如果有人选择了比应用样式更大的区域,则需要将选择分为两部分:完全与样式区域重叠的部分和根本不与样式区域重叠的部分。您需要逐个字符地查找在哪里分解选择,但至少您不必将样式应用于每个单独的字符。

It sounds like character by character is your safest bet. It doesn't seem like a significant problem unless you're creating a full-blown word processor.

The other alternative is to break up selections based on current styling. So, if someone selects a larger region than a style is applied to, you need to break up the selection into two parts: the part that completely overlaps the styled area and the part that does not overlap the styled area at all. You'll need to go character by character to find out where to break up the selections, but at least you won't have to necessarily apply styles to every individual character.

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