太字、斜体、下線、およびそれらの任意の組み合わせを使用して、WORD などのスタイルを設定できるようにしたいリッチ テキスト ボックスがあります。
以下のコードを使用して BOLD スタイリングを追加および削除できます。また、複数のスタイリングを追加することもできますが、複数のスタイリングを設定して 1 つを削除しようとしても何も起こりません。
スタイル変更コード:
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
いつものように、あなたの助けは大歓迎です!