我在调整 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
这是此代码的最终问题:
- 我切换粗体(为真) - 文本变为粗体。
- 我切换下划线 - 文本现在是粗体和下划线。
- 我切换斜体 - 文本现在是粗体、下划线和斜体。
- 我再次切换粗体(变为假) - 文本现在是删除线。
字体发生了什么?正文应加下划线,斜体不加删除线...
这是我的逻辑错误还是简单的误解?
好吧,谢谢您抽出宝贵时间,我会继续修改它,直到它起作用或者我得到一个有效的答案,