在 vb.net 中调整 RichTextBox 的字体样式

发布于 2024-12-22 12:13:30 字数 1776 浏览 3 评论 0原文

我在调整 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2024-12-29 12:13:30

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

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:

    Dim style = Me.Font.Style
    '--- turn bold on
    style = style Or FontStyle.Bold
    '--- turn bold off
    style = style And Not FontStyle.Bold
李不 2024-12-29 12:13:30

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

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

感谢您抽出时间!

Well, I've got it. I've gotten it to work successfully.

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

Thanks for your time!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文