setting Font property of Label control in code

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

This is a very basic question here. I must be missing something
here.

At run time, I am trying to set the text of the label control bold.

lblMyLabel.Font.Bold = true;

does not work since it's read only (why why why why why).

Thank you.
 
Hello David,
At run time, I am trying to set the text of the label control bold.

lblMyLabel.Font.Bold = true;

does not work since it's read only

Use a line like this instead:

label1.Font.Bold = new Font(label1.Font, FontStyle.Bold);


Oliver Sturm
 
Hello David,
Thank you. I can't think of a rhyme or reason why Font.Bold property
should be read only. Can you?

Well... sure ;-)

The whole Font handling goes back onto Windows resources outside .NET. On
the API level it's simply not possible to set a property Bold on the font
- after all, for a lot of fonts it's a different font resource that is
being loaded for bold or non-bold display. Check your Windows\Fonts folder
and you'll see that there are several arial*.ttf files - one of them is
the bold version of Arial.

Of course the .NET API could theoretically hide that detail from you and
make the Bold property writable nevertheless. But they decided not to go
that way and I can understand why - the way things are, the user of the
API understands that he's triggering what may be a costly operation.
Otherwise it would have seemed like switching the Bold flag may be a
trivial thing without a lot of overhead.


Oliver Sturm
 
Back
Top