一种克服 RichTextBox 局限性的方法? [英] A way to overcome RichTextBox's limitations?

查看:47
本文介绍了一种克服 RichTextBox 局限性的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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?

谢谢.

推荐答案

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

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.

这篇关于一种克服 RichTextBox 局限性的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆