vb net去除富文本框中的部分字体样式

vb net Remove partial font style in rich text box

我有一个富文本框,我希望它能够像 WORD 等一样使用粗体、斜体、下划线和它们的任意组合。
我可以使用下面的代码添加和删除 BOLD 样式,我也可以添加多个样式,但是如果我设置了多个样式并尝试删除一个,则什么也不会发生。

样式更改代码:

Private Sub Underline_Text(rtBox As RichTextBox)
    Dim newStyle As FontStyle
    If rtBox.SelectionFont.Style = FontStyle.Underline Then
        newStyle = rtBox.SelectionFont.Style And Not FontStyle.Underline
    Else
        newStyle = rtBox.SelectionFont.Style Or FontStyle.Underline
    End If
    Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
    rtBox.SelectionFont = newFont
End Sub

完整代码:

Private Sub rtbDesc_KeyDown(sender As Object, e As KeyEventArgs) Handles rtbDesc.KeyDown
    If e.Control AndAlso Not e.Alt AndAlso Not e.Shift Then
        Select Case e.KeyCode.ToString
            Case "B"
                Bold_Text(DirectCast(sender, RichTextBox))
                e.SuppressKeyPress = True
            Case "I"
                Italics_Text(DirectCast(sender, RichTextBox))
                e.SuppressKeyPress = True
            Case "U"
                Underline_Text(DirectCast(sender, RichTextBox))
                e.SuppressKeyPress = True
            Case "R"
                Reset_Text(DirectCast(sender, RichTextBox))
                e.SuppressKeyPress = True
            Case "K"
                Strikeout_Text(DirectCast(sender, RichTextBox))
                e.SuppressKeyPress = True
        End Select
    End If
End Sub
Private Sub Bold_Text(rtBox As RichTextBox)
    Dim newStyle As FontStyle
    If rtBox.SelectionFont.Style = FontStyle.Bold Then
        newStyle = rtBox.SelectionFont.Style And Not FontStyle.Bold
    Else
        newStyle = rtBox.SelectionFont.Style Or FontStyle.Bold
    End If
    Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
    rtBox.SelectionFont = newFont
End Sub
Private Sub Italics_Text(rtBox As RichTextBox)
    Dim newStyle As FontStyle
    If rtBox.SelectionFont.Style = FontStyle.Italic Then
        newStyle = rtBox.SelectionFont.Style And Not FontStyle.Italic
    Else
        newStyle = rtBox.SelectionFont.Style Or FontStyle.Italic
    End If
    Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
    rtBox.SelectionFont = newFont
End Sub
Private Sub Underline_Text(rtBox As RichTextBox)
    Dim newStyle As FontStyle
    If rtBox.SelectionFont.Style = FontStyle.Underline Then
        newStyle = rtBox.SelectionFont.Style And Not FontStyle.Underline
    Else
        newStyle = rtBox.SelectionFont.Style Or FontStyle.Underline
    End If
    Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
    rtBox.SelectionFont = newFont
End Sub
Private Sub Reset_Text(rtBox As RichTextBox)
    Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, FontStyle.Regular)
    rtBox.SelectionFont = newFont
End Sub
Private Sub Strikeout_Text(rtBox As RichTextBox)
    Dim newStyle As FontStyle
    If rtBox.SelectionFont.Style = FontStyle.Strikeout Then
        newStyle = rtBox.SelectionFont.Style And Not FontStyle.Strikeout
    Else
        newStyle = rtBox.SelectionFont.Style Or FontStyle.Strikeout
    End If
    Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
    rtBox.SelectionFont = newFont
End Sub  

一如既往,非常感谢您的帮助!

伙计,这是一个棘手的问题。

我复制了您的代码并在您的 Bold_Text 方法中放置了一个断点。我发现你的 If 语句没有检查 Bold and Underline 条件。这是我的意思:

If rtBox.SelectionFont.Style = FontStyle.Underline Then

当我添加断点时,当试图删除粗体时,它跳过了这个条件并转到 Else 子句,因为你只检查它是否是粗体,而不是粗体 下划线。在 If 语句之前添加一个 MessageBox 以查看它正在检测像这样应用的粗体和下划线样式:

MessageBox.Show(rtBox.SelectionFont.Style.ToString)

表示rtBox.SelectionFont.Style设置为"Bold, Underline"。

为了修复它,我使用了这段代码:

If (rtBox.SelectionFont.Style = FontStyle.Bold) OrElse (rtBox.SelectionFont.Style = (FontStyle.Bold Or FontStyle.Underline)) Then

请参阅 this link 了解更多解释。

The FontStyle enumeration is a flags enumeration, so you can combine values (using the Or operator in VB.NET...

这是我最终解决问题的方法。
我没有尝试所有样式组合,而是使用了 Sylverac 的技巧,将样式发送到字符串并简单地检查返回的字符串中相关样式的关键字等。

代码:

Private Sub Bold_Text(rtBox As RichTextBox)
    Dim newStyle As FontStyle
    If InStr(rtBox.SelectionFont.Style.ToString, "Bold") Then 'Changed this line to search the string
        newStyle = rtBox.SelectionFont.Style And Not FontStyle.Bold
    Else
        newStyle = rtBox.SelectionFont.Style Or FontStyle.Bold
    End If
    Dim newFont As New Font(rtBox.SelectionFont.Name, rtBox.SelectionFont.Size, newStyle)
    rtBox.SelectionFont = newFont
End Sub