Text won't change back to Regular from Bold

  • Thread starter Thread starter fidtz
  • Start date Start date
F

fidtz

Sorry for asking here, but I don't know enough dotnet or Windows
programming yet to be very good at googling for answers. OTOH, this is
the first question I haven't be able to resolve myself, the
documentations is excellent.

This code looks to me like it should swap the bold highlight around,
but Regular never gets reapplied, leaving both labels in Bold.

Maybe there is a better way of doing this (aside from the obvious
refactor), but I can't see why this doesn't work and maybe the answer
will teach me of wider issues I should know about :)

void Label1Click(object sender, System.EventArgs e)
{
label1.Font = new Font(label1.Font, label1.Font.Style |
FontStyle.Bold);
label2.Font = new Font(label2.Font, label2.Font.Style |
FontStyle.Regular);
}
void Label2Click(object sender, System.EventArgs e)
{
label1.Font = new Font(label1.Font, label1.Font.Style |
FontStyle.Regular);
label2.Font = new Font(label2.Font, label2.Font.Style |
FontStyle.Bold);
}

label1 and label2 are two Windows Forms labels.
 
Hey...

Use this:


void Label1Click( object sender, System.EventArgs e )
{
label1.Font =
new Font( label1.Font, label1.Font.Style ^ FontStyle.Bold );
}
void Label2Click( object sender, System.EventArgs e )
{
label2.Font =
new Font( label2.Font, label2.Font.Style ^ FontStyle.Bold );
}



Explanation:

^ = logical XOR.
So when the bold flag is of (bit 0), XOR with bold (bit 1) will put it
on. If bold is on (bit 1), XOR with bold (also bit 1) will put it off.
Because all other bits in the FontStyle.Bold are 0, other styles are not
influenced.

Your code went wrong, because FontStyle.Regular has value 0. Using |
(logical OR) with 0 has no influence whatsoever. Once bold, your label
stays bold.


David
 
David said:
Hey...

Use this:


void Label1Click( object sender, System.EventArgs e )
{
label1.Font =
new Font( label1.Font, label1.Font.Style ^ FontStyle.Bold );
}
void Label2Click( object sender, System.EventArgs e )
{
label2.Font =
new Font( label2.Font, label2.Font.Style ^ FontStyle.Bold );
}



Explanation:

^ = logical XOR.
So when the bold flag is of (bit 0), XOR with bold (bit 1) will put it
on. If bold is on (bit 1), XOR with bold (also bit 1) will put it off.
Because all other bits in the FontStyle.Bold are 0, other styles are not
influenced.

Your code went wrong, because FontStyle.Regular has value 0. Using |
(logical OR) with 0 has no influence whatsoever. Once bold, your label
stays bold.


David

Brilliant, thanks for the explanation and solution. That would explain
some of the code I found that seemed tantalisingly close to what I
needed :) Now I can see the "FlagsAttribute" stuff in the docs :)

Dom.
 
You can't use "regular", as the bitwise value of regular is 0;

Regular = 0,
Bold = 1,
Italic = 2,
Underline = 4,
Strikeout = 8,

Instead, you must use "and not (things to exclude)"

To remove bold and italic, use (where fs is the font-style).
fs = fs & ~(FontStyle.Bold | FontStyle.Italic);
Obviously to just remove bold, use
fs = fs & ~(FontStyle.Bold);

Marc
 
Late reply by me...

As per previous reply by David, XOR will keep toggling the bit... the "and
not" approach exclusively removes a bit, so is more comparable to the OR
that you are currently using. Of course, with the XOR approach you only need
one handler, not two ;-p

Marc
 
Marc said:
Late reply by me...

As per previous reply by David, XOR will keep toggling the bit... the "and
not" approach exclusively removes a bit, so is more comparable to the OR
that you are currently using. Of course, with the XOR approach you only need
one handler, not two ;-p

Marc

I will use the XOR once I refactor this mess I am calling a dialog, but
yours works better as an in place replacement, thanks!

Dom
 
Back
Top