1

太字、斜体、下線、およびそれらの任意の組み合わせを使用して、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  

いつものように、あなたの助けは大歓迎です!

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 を追加して、次のように適用された Bold スタイルと Underline スタイルの両方が検出されていることを確認します。

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 に答える