我有一个富文本框,我希望能够使用粗体、斜体、下划线和它们的任意组合来设置 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
一如既往,非常感谢您的帮助!