WoodChuckChuck
WoodChuckChuck

Reputation: 69

vb net Remove partial font style in rich text box

I have a rich text box that I want to be able to style like WORD etc with BOLD, ITALIC, UNDERLINE and any combination of them.
I can add and remove say BOLD styling with the code below and I can also add multiple stylings but if I have multiple stylings set and try to remove one nothing happens.

Style Change Code:

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

Full Code:

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  

As always, your help is greatly appreciated!

Upvotes: 1

Views: 1577

Answers (2)

WoodChuckChuck
WoodChuckChuck

Reputation: 69

This was the way I ultimately resolved my issue FWIW.
Instead of trying all combinations of styles I used Sylverac's trick of sending the style to a string and simply checking for the keyword of the style in question in that returned string etc.

Code:

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

Upvotes: 1

Lews Therin
Lews Therin

Reputation: 3777

Man this was a tricky one.

I copied your code and put a breakpoint in your Bold_Text method. What I found is your If statement is not checking for the Bold and Underline condition. Here's the code I meant:

If rtBox.SelectionFont.Style = FontStyle.Underline Then

When I added the breakpoint, when trying to remove the bold, it skipped this condition and went to the Else clause because you only check to see if it is bold, not bold and underlined. Add a MessageBox before the If statement to see that it is detecting both Bold and Underline styles applied like this:

MessageBox.Show(rtBox.SelectionFont.Style.ToString)

That shows that rtBox.SelectionFont.Style is set to "Bold, Underline".

To fix it, I used this code:

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

See this link for a little more of an explanation.

The FontStyle enumeration is a flags enumeration, so you can combine values (using the Or operator in VB.NET...

Upvotes: 0

Related Questions