So many fonts ...

  • Thread starter Thread starter Gerhard Menzl
  • Start date Start date
G

Gerhard Menzl

I have noticed that the Windows Forms Designer generates code that
creates a new System.Drawing.Font object every time the Font property of
a control is set to any value other than the default. Isn't this a
terrible waste of system resources? Or are those mere flyweight objects,
and the framework prevents needless resource duplication behind the
scenes anyway?
 
* Gerhard Menzl said:
I have noticed that the Windows Forms Designer generates code that
creates a new System.Drawing.Font object every time the Font property
of a control is set to any value other than the default. Isn't this a
terrible waste of system resources? Or are those mere flyweight
objects, and the framework prevents needless resource duplication
behind the scenes anyway?

Assigning different font objects has one advantage: Changing the font
of one control doesn't affect other (nested) controls.
 
Herfried K. Wagner said:
Assigning different font objects has one advantage: Changing the font
of one control doesn't affect other (nested) controls.

Are fonts not immutable? There's a big difference between changing
which font reference the Font property of the control has and making
changes to the font object itself. If, as I suspect, font objects are
immutable, I don't see the advantage of creating several objects
representing the same font. Could you give an example of how you think
it helps?
 
* Jon Skeet said:
Are fonts not immutable?

They are immutable.
There's a big difference between changing
which font reference the Font property of the control
has and making changes to the font object itself.

It's not possible to make changes to the 'Font' object itself.
If, as I suspect, font objects are immutable, I don't see the
advantage of creating several objects representing the same font.

Well, add a groupbox to your form and add a label to it. Then add code
to a button that changes the font of the groupbox at runtime. Run the
application. You will see that the font of the label is changed when
pressing the button that changes the font of the groupbox. In this
case, assigning another font object to the label with the same settings
will prevent the label's font from being changed.
 
Herfried K. Wagner said:
They are immutable.


It's not possible to make changes to the 'Font' object itself.


Well, add a groupbox to your form and add a label to it. Then add code
to a button that changes the font of the groupbox at runtime. Run the
application. You will see that the font of the label is changed when
pressing the button that changes the font of the groupbox. In this
case, assigning another font object to the label with the same settings
will prevent the label's font from being changed.

Is there any need to create multiple actual objects though? Won't

Font f = ...;
groupbox.Font = f;
label.Font = f;

do just as well? Note that the OP wasn't complaining about fonts being
explicitly set - he was complaining that multiple font objects were
being created redundantly.
 
* Jon Skeet said:
Is there any need to create multiple actual objects though? Won't

Font f = ...;
groupbox.Font = f;
label.Font = f;

do just as well?

This will fix the problem. I agree that creating more than one font
object for the same font doesn't make much sense.
 
Jon said:
Is there any need to create multiple actual objects though? Won't

Font f = ...;
groupbox.Font = f;
label.Font = f;

do just as well? Note that the OP wasn't complaining about fonts being
explicitly set - he was complaining that multiple font objects were
being created redundantly.

That's exactly what I would like to do. For example, I want the
listboxes in all my dialogs to appear in the same font (which does not
happen to be the default font). A separate Font object for each of them
is actually a big disadvantage because if I were to change the font, I
would have to do so in dozens of places.

Having started programming Windows applications in C and on Windows 3.0,
I have been trained to avoid the waste of graphic resources. The Windows
Forms Designer, on the other hand, doesn't seem to bother and will
merrily create new font objects like there is no tomorrow.

So is there any problem with creating a single Font object for listboxes
and use it on every form (apart from losing designer support through
hand-coding, which is inevitable anyway in non-trivial GUI programming)?
 
Back
Top