3

I'm having trouble adjusting the font style in a RichTextBox and I've seen a few different approaches that talk about single attributes (like toggling bold on and off)... but I'm trying to make it so that my font class can adjust any attribute (bold, italic, underline).

I realize that Font.Style is a set of Boolean flags (a bitfield?)... but I'm not sure how to handle the attributes all at once.

Here is the troublesome code:

Public Sub ModifyFontStyle(Optional ByVal Plain As Object = Nothing, Optional ByVal Bold As Object = Nothing, _
                           Optional ByVal Italics As Object = Nothing, Optional ByVal Underlined As Object = Nothing)
    Dim newFontStyle As System.Drawing.FontStyle


    If Plain Then
        newFontStyle = Drawing.FontStyle.Regular
        GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle)
        Exit Sub
    End If

    If Bold IsNot Nothing Then
        If Bold Then
            newFontStyle = GivenFont.Style + Drawing.FontStyle.Bold
        Else
            newFontStyle = GivenFont.Style - Drawing.FontStyle.Bold
        End If
    End If


    If Italics IsNot Nothing Then
        If Italics Then
            newFontStyle = GivenFont.Style + Drawing.FontStyle.Italic
        Else
            newFontStyle = GivenFont.Style - Drawing.FontStyle.Italic
        End If
    End If

    If Underlined IsNot Nothing Then
        If Underlined Then
            newFontStyle = GivenFont.Style + Drawing.FontStyle.Underline
        Else
            newFontStyle = GivenFont.Style - Drawing.FontStyle.Underline
        End If
    End If

    GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle)

End Sub

And here is the eventual trouble with this code:

  • I toggle bold (to true) - the text goes bold.
  • I toggle underlined - the text is now bold and underlined.
  • I toggle italics - the text is now bold, underlined, and in italics.
  • I toggle bold again (to false) - the text is now strikethrough.

What is happening to the font? The text should be underlined and in italics not strikethrough...

Is this a logic error or a simple misunderstanding on my part?

Well, thank you for your time, I'll keep tinkering around with it until it is working or I get an answer that works,

Sameera Thilakasiri
  • 9,452
  • 10
  • 51
  • 86
Dominick
  • 59
  • 2
  • 5

2 Answers2

3

You are using the wrong operators. They are indeed similar to bit flags, the enum has the [Flags] attribute. You'll need to use the Or operator to turn a style on and the And operator to turn a style off. Like this:

    Dim style = Me.Font.Style
    '--- turn bold on
    style = style Or FontStyle.Bold
    '--- turn bold off
    style = style And Not FontStyle.Bold
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

Well, I've got it. I've gotten it to work successfully.

Public Sub ModifyFontStyle(Optional ByVal Plain As Object = Nothing, Optional ByVal Bold As Object = Nothing, _
                           Optional ByVal Italics As Object = Nothing, Optional ByVal Underlined As Object = Nothing)
    Dim newFontStyle As System.Drawing.FontStyle


    If Plain Then
        newFontStyle = Drawing.FontStyle.Regular
        GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle)
        Exit Sub
    End If

    If Bold IsNot Nothing Then
        If Bold And Not GivenFont.Bold Then
            newFontStyle = GivenFont.Style + Drawing.FontStyle.Bold
        Else
            newFontStyle = GivenFont.Style - Drawing.FontStyle.Bold
        End If
    End If


    If Italics IsNot Nothing Then
        If Italics And Not GivenFont.Italic Then
            newFontStyle = GivenFont.Style + Drawing.FontStyle.Italic
        Else
            newFontStyle = GivenFont.Style - Drawing.FontStyle.Italic
        End If
    End If

    If Underlined IsNot Nothing Then
        If Underlined And Not GivenFont.Underline Then
            newFontStyle = GivenFont.Style + Drawing.FontStyle.Underline
        Else
            newFontStyle = GivenFont.Style - Drawing.FontStyle.Underline
        End If
    End If

    GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle)

End Sub

Thanks for your time!

Dominick
  • 59
  • 2
  • 5