vb net在富文本框中删除部分字体样式

时间:2015-10-07 17:13:27

标签: vb.net richtextbox rtf text-styling

我有一个富文本框,我希望能够像BORD,ITALIC,UNDERLINE以及它们的任意组合一样使用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  

与往常一样,非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

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

代码:

Point

答案 1 :(得分: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

请参阅this link了解更多信息。

  

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