Text won't change back to Regular from Bold

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.
 
D

David Boucherie & Co

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
 
F

fidtz

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.
 
M

Marc Gravell

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
 
M

Marc Gravell

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
 
F

fidtz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top