3

我在调整 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

这是此代码的最终问题:

  • 我切换粗体(为真) - 文本变为粗体。
  • 我切换下划线 - 文本现在是粗体和下划线。
  • 我切换斜体 - 文本现在是粗体、下划线和斜体。
  • 我再次切换粗体(变为假) - 文本现在是删除线。

字体发生了什么?正文应加下划线,斜体不加删除线...

这是我的逻辑错误还是简单的误解?

好吧,谢谢您抽出宝贵时间,我会继续修改它,直到它起作用或者我得到一个有效的答案,

4

2 回答 2

3

您使用了错误的运算符。它们确实类似于位标志,枚举具有 [Flags] 属性。您需要使用 Or 运算符打开样式,使用 And 运算符关闭样式。像这样:

    Dim style = Me.Font.Style
    '--- turn bold on
    style = style Or FontStyle.Bold
    '--- turn bold off
    style = style And Not FontStyle.Bold
于 2011-12-23T16:45:34.103 回答
0

嗯,我明白了。我已经让它成功工作了。

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 And Not GivenFont.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 And Not GivenFont.Italic 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 And Not GivenFont.Underline 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

谢谢你的时间!

于 2011-12-23T20:26:55.430 回答