Properties of Font-class are all read-only ???

C

Christian

Hi,

the following produces a compiler error :

label1.Font.Size = 10

==> ERROR:property or indexer 'System.Drawing.Font.Size' cannot be
assigned to -- it is read only

OK, but why has it been designed as read-only. As are all properties of the
Font-class by the way (???)

Is there no way I can set the individual properties of a Font-object ?

thnx

Chris
 
J

Jay B. Harlow [MVP - Outlook]

Christian,
To set a property on a Font object you need to create a new Font object,
passing the new value.

I suspect it was designed this way to avoid "aliasing" problems. Aliasing
refers to when two objects (Label1 & Label2) both share a single Font
object, changing the Font object (property) from the first also changes the
second. For example: You changed Label1.Font.Size and suddenly
Label2.Font.Size is also changed...

Hope this helps
Jay
 
H

Herfried K. Wagner [MVP]

* "Jay B. Harlow said:
To set a property on a Font object you need to create a new Font object,
passing the new value.

I suspect it was designed this way to avoid "aliasing" problems. Aliasing
refers to when two objects (Label1 & Label2) both share a single Font
object, changing the Font object (property) from the first also changes the
second. For example: You changed Label1.Font.Size and suddenly
Label2.Font.Size is also changed...

Maybe another reason is that if changing properties was possible, inside the
class a new font must be created (Win32 GDI font "objects" cannot be
changed after creation, AFAIR).
 
J

Jay B. Harlow [MVP - Outlook]

Herfried,
I suspect you are (more) correct, I noticed a MSFT post in the C# newsgroup
stated the same thing. (that Win32 GDI font "objects" cannot be changed).

Considering creating a new font for each property change could get
expensive, especially if you are changing all 12 or 13 properties (12 or 13
Win32 GDI font "objects" would wind up getting created).

Jay
 

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