【问题标题】:Adjusting font style for a RichTextBox in vb.net在 vb.net 中调整 RichTextBox 的字体样式
【发布时间】:2012-01-26 15:03:23
【问题描述】:

我在调整 RichTextBox 中的字体样式时遇到了麻烦,我已经看到了一些不同的方法来讨论单个属性(比如切换粗体和关闭)......但我正在努力做到这一点我的字体类可以调整 any 属性(粗体、斜体、下划线)。

我知道 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

这是这段代码的最终问题:

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

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

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

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

【问题讨论】:

  • 我想出了我需要做的事情:检查我正在调整的特定属性并阻止它多次添加到字体样式中。稍后我会发布这个问题的答案。
  • 标志被移除,请参考FAQ。

标签: vb.net fonts richtextbox adjustment


【解决方案1】:

您使用了错误的运算符。它们确实类似于位标志,枚举具有 [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

【讨论】:

    【解决方案2】:

    嗯,我明白了。我已经让它成功运行了。

    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
    

    感谢您的宝贵时间!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多