Label Font Size - Read Only?

bjwade62

Centurion
Joined
Oct 31, 2003
Messages
104
Anyone know why Label.font.size and Label.font.bold are read only at runtime in .NET?

Is there a work around?
 
There is no "work around." This is not a bug, it is by design. (Do I sound like I work at Microsoft?) Font objects are "Immutable," i.e. you can not modify them once created, just like strings (notice that all string functions return a new string object). In order to change the size or style of a font you must create a new System.Drawing.Font object.

Fortunately Microsoft made made a constructor that accepts a prototype font and allows you to specify a style, like so:
Visual Basic:
' Make this control's font bold, not italic, not underlined, and not strikethru:
Me.Font = New Font(this.Font, FontStyle.Bold)
If you need to change size too then you need to use a normal constructor.
Visual Basic:
' Make this control's font size 16 bold italic
Me.Font = New Font(Me.Font.FontFamily, 16, FontStyle.Bold or FontStyle.Italic)
 
Many Thanks.

marble_eater said:
There is no "work around." This is not a bug, it is by design. (Do I sound like I work at Microsoft?) Font objects are "Immutable," i.e. you can not modify them once created, just like strings (notice that all string functions return a new string object). In order to change the size or style of a font you must create a new System.Drawing.Font object.

Fortunately Microsoft made made a constructor that accepts a prototype font and allows you to specify a style, like so:
Visual Basic:
' Make this control's font bold, not italic, not underlined, and not strikethru:
Me.Font = New Font(this.Font, FontStyle.Bold)
If you need to change size too then you need to use a normal constructor.
Visual Basic:
' Make this control's font size 16 bold italic
Me.Font = New Font(Me.Font.FontFamily, 16, FontStyle.Bold or FontStyle.Italic)
 
Back
Top