1

我有一个富文本框,我希望能够使用粗体、斜体、下划线和它们的任意组合来设置 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  

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

4

2 回答 2

1

这就是我最终解决问题 FWIW 的方式。
我没有尝试所有样式组合,而是使用了 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
于 2015-10-07T21:50:43.273 回答
0

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

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设置为“粗体,下划线”。

为了修复它,我使用了以下代码:

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

有关更多解释,请参阅此链接。

FontStyle 枚举是一个标志枚举,因此您可以组合值(使用 VB.NET 中的 Or 运算符...

于 2015-10-07T18:08:33.687 回答