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,