I've been given the following method:
Private Sub boldButton_Click(sender As System.Object, e As System.EventArgs) Handles boldButton.Click
Dim curFont As Font
Dim newFont As Font
curFont = rtb.SelectionFont
If curFont IsNot Nothing Then
'create the new font
newFont = New Font(curFont.FontFamily, curFont.Size, curFont.Style Xor FontStyle.Bold)
'set it
rtb.SelectionFont = newFont
End If
End Sub
Currently having problems understanding what is happening with this part of the code curFont.Style Xor FontStyle.Bold
. What is a valid way to achieve the same result without using the operator
Xor
?
EDIT (As commented by us2012) Do I need an alternative?
I've looked up Xor on MSDN but still having trouble understanding the implementation of it in boldButton_Click
procedure.
Bitwise XOR toggles a flag. Let's assume that the Style bitfield looks like this
00000000
^^^
BIU (Bold, Italic, Underline)
So the value of FontStyle.Bold
would be:
00000100
Now something Xor FontStyle.Bold
will just flip this bit in something
. Example:
00000111 Xor 00000100 = 00000011 (Boldness removed)
00000001 Xor 00000100 = 00000101 (Boldness added)
Note that the other bits are unaffected.
Since you explicitly asked for alternatives: You could check whether the bit is set style And Bold <> 0
, and then either set it style = style Or Bold
or remove it style = style And (Not Bold)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With