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!

Published Wednesday, February 22, 2006 8:04 AM by KenG

Comments

Thursday, February 23, 2006 5:49 AM by @ Head

# The faceless font

I was chatting with Ken Getz the other day about the RichTextBox. Ken pointed out to me that the SelectionFont...