More on Toggling Font Styles
Soon after posting the previous item, my VB pal from down under, Bill McCarthy, responded quickly with a suggested upgrade. We went back and forth a few times, and here's what I ended up with:
Private Sub ToggleFormat(ByVal rtb As RichTextBox, ByVal newStyle As FontStyle)
If rtb IsNot Nothing Then
Dim fnt As Font = rtb.SelectionFont
If fnt IsNot Nothing Then
rtb.SelectionFont = New Font(fnt, fnt.Style Xor newStyle)
End If
End If
End Sub
Originally, I had balked at the use of Xor (mostly 'cause I'm writing this up in courseware for beginners) but Bill reminded me that that's even a BETTER reason to use Xor--to show folks how they can use it in real code. Thanks, Bill, for the easy and elegant code!