在 vb.net 中调整 RichTextBox 的字体样式
我在调整 RichTextBox 中的字体样式时遇到问题,并且我看到了几种讨论单个属性的不同方法(例如打开和关闭粗体)...但我正在尝试这样做,以便我的字体类可以调整任何属性(粗体、斜体、下划线)。
我意识到 Font.Style 是一组布尔标志(一个位字段?)...但我不确定如何同时处理所有属性。
这是麻烦的代码:
Public Sub ModifyFontStyle(Optional ByVal Plain As Object = Nothing, Optional ByVal Bold As Object = Nothing, _
Optional ByVal Italics As Object = Nothing, Optional ByVal Underlined As Object = Nothing)
Dim newFontStyle As System.Drawing.FontStyle
If Plain Then
newFontStyle = Drawing.FontStyle.Regular
GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle)
Exit Sub
End If
If Bold IsNot Nothing Then
If Bold Then
newFontStyle = GivenFont.Style + Drawing.FontStyle.Bold
Else
newFontStyle = GivenFont.Style - Drawing.FontStyle.Bold
End If
End If
If Italics IsNot Nothing Then
If Italics Then
newFontStyle = GivenFont.Style + Drawing.FontStyle.Italic
Else
newFontStyle = GivenFont.Style - Drawing.FontStyle.Italic
End If
End If
If Underlined IsNot Nothing Then
If Underlined Then
newFontStyle = GivenFont.Style + Drawing.FontStyle.Underline
Else
newFontStyle = GivenFont.Style - Drawing.FontStyle.Underline
End If
End If
GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle)
End Sub
这是此代码的最终问题:
- 我将粗体切换为真 - 文本变为粗体。
- 我切换下划线 - 文本现在为粗体并带有下划线。
- 我切换斜体 - 文本现在为粗体、下划线和斜体。
- 我再次切换粗体(为 false) - 文本现在带有删除线。
字体发生了什么?文本应该加下划线,斜体而不是删除线......
这是一个逻辑错误还是我的一个简单的误解?
好吧,谢谢您的宝贵时间,我会继续修改它,直到它起作用或者我得到一个有效的答案,
I'm having trouble adjusting the font style in a RichTextBox and I've seen a few different approaches that talk about single attributes (like toggling bold on and off)... but I'm trying to make it so that my font class can adjust any attribute (bold, italic, underline).
I realize that Font.Style is a set of Boolean flags (a bitfield?)... but I'm not sure how to handle the attributes all at once.
Here is the troublesome code:
Public Sub ModifyFontStyle(Optional ByVal Plain As Object = Nothing, Optional ByVal Bold As Object = Nothing, _
Optional ByVal Italics As Object = Nothing, Optional ByVal Underlined As Object = Nothing)
Dim newFontStyle As System.Drawing.FontStyle
If Plain Then
newFontStyle = Drawing.FontStyle.Regular
GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle)
Exit Sub
End If
If Bold IsNot Nothing Then
If Bold Then
newFontStyle = GivenFont.Style + Drawing.FontStyle.Bold
Else
newFontStyle = GivenFont.Style - Drawing.FontStyle.Bold
End If
End If
If Italics IsNot Nothing Then
If Italics Then
newFontStyle = GivenFont.Style + Drawing.FontStyle.Italic
Else
newFontStyle = GivenFont.Style - Drawing.FontStyle.Italic
End If
End If
If Underlined IsNot Nothing Then
If Underlined Then
newFontStyle = GivenFont.Style + Drawing.FontStyle.Underline
Else
newFontStyle = GivenFont.Style - Drawing.FontStyle.Underline
End If
End If
GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle)
End Sub
And here is the eventual trouble with this code:
- I toggle bold (to true) - the text goes bold.
- I toggle underlined - the text is now bold and underlined.
- I toggle italics - the text is now bold, underlined, and in italics.
- I toggle bold again (to false) - the text is now strikethrough.
What is happening to the font? The text should be underlined and in italics not strikethrough...
Is this a logic error or a simple misunderstanding on my part?
Well, thank you for your time, I'll keep tinkering around with it until it is working or I get an answer that works,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用了错误的运算符。它们确实类似于位标志,枚举具有 [Flags] 属性。您需要使用 Or 运算符来打开样式,并使用 And 运算符来关闭样式。像这样:
You are using the wrong operators. They are indeed similar to bit flags, the enum has the [Flags] attribute. You'll need to use the Or operator to turn a style on and the And operator to turn a style off. Like this:
嗯,我明白了。我已经让它成功工作了。
感谢您抽出时间!
Well, I've got it. I've gotten it to work successfully.
Thanks for your time!