A flexible way of this is to use the FontStyle as a flag i.e. if the font should be bold and underlined the style is FontStyle.Bold And FontStyle.Underline.
A very thrown together implementation would be:
- Private Sub AddStyle(ByVal style As FontStyle)
-
Dim newStyle As FontStyle = testRich.SelectionFont.Style Or style
-
Dim ifont As New Font(testRich.Font, newStyle)
-
testRich.SelectionFont = ifont
-
End Sub
-
-
Private Sub RemoveStyle(ByVal style As FontStyle)
-
Dim newStyle As FontStyle = testRich.SelectionFont.Style Xor style
-
Dim ifont As New Font(testRich.Font, newStyle)
-
testRich.SelectionFont = ifont
-
End Sub
-
-
Private Function HasStyle(ByVal style As FontStyle) As Boolean
-
Return (testRich.SelectionFont.Style And style) = style
-
End Function
This assumes that the RichTextBox control is called testRich. Calling these procedures is done by:
- ' HasStyle(FontStyle.Bold) = False
-
AddStyle(FontStyle.Bold)
-
-
' HasStyle(FontStyle.Bold) = True
-
RemoveStyle(FontStyle.Bold)
-
-
' HasStyle(FontStyle.Bold) = False
The And, Or & XOr (Exclusive Or) are all bitwise operator. A brief tutorial can be seen
here.
Using this method means that you can use any of the FontStyle values.