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

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".

Re: So many fonts ... by hirf-spam-me-here

hirf-spam-me-here
Wed Jun 23 10:31:05 CDT 2004

* Gerhard Menzl <gerhard.menzl@spambucket.net> scripsit:
> 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 [MVP]
<URL:http://dotnet.mvps.org/>

Re: So many fonts ... by Jon

Jon
Wed Jun 23 10:55:11 CDT 2004

Herfried K. Wagner [MVP] <hirf-spam-me-here@gmx.at> wrote:
> * Gerhard Menzl <gerhard.menzl@spambucket.net> scripsit:
> > 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.

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: So many fonts ... by hirf-spam-me-here

hirf-spam-me-here
Wed Jun 23 11:22:12 CDT 2004

* Jon Skeet [C# MVP] <skeet@pobox.com> scripsit:
>> Assigning different font objects has one advantage: Changing the font
>> of one control doesn't affect other (nested) controls.
>
> 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 [MVP]
<URL:http://dotnet.mvps.org/>

Re: So many fonts ... by Jon

Jon
Wed Jun 23 11:58:10 CDT 2004

Herfried K. Wagner [MVP] <hirf-spam-me-here@gmx.at> wrote:
> * Jon Skeet [C# MVP] <skeet@pobox.com> scripsit:
> >> Assigning different font objects has one advantage: Changing the font
> >> of one control doesn't affect other (nested) controls.
> >
> > 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.

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: So many fonts ... by hirf-spam-me-here

hirf-spam-me-here
Wed Jun 23 13:42:46 CDT 2004

* Jon Skeet [C# MVP] <skeet@pobox.com> scripsit:
>> 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?

This will fix the problem. I agree that creating more than one font
object for the same font doesn't make much sense.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Re: So many fonts ... by Gerhard

Gerhard
Thu Jun 24 03:00:29 CDT 2004

Jon Skeet [C# MVP] wrote:

> 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)?

--
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".