Continue to my previous question (link)
What if I want to assign the font of a new user control to its creator's font. Should I do this a):
newControl = new MyControl(...);
...
newControl.Font = this.Font;
or this b)?
newControl = new MyControl(...);
...
newControl.Font = (Font)this.Font.Clone();
If answer is a), what will happen to the new user control's font if the creator's font get disposed (for example, a window closed by user)?
Thanks,
Gilbert
The Font
class actually encapsulates two things:
A text style
A GDI handle that may be used to draw text with that style
The text style encapsulated by a Font
class is immutable; the handle is "disposable immutable", meaning that it will never encapsulate any GDI handle other than the one with which it was created, but once the Font
is disposed it will cease to encapsulate any font handle (it becomes truly immutable, albeit useless, at that point).
Setting the Font
property of the controls in the Framework will cause it to grab two things:
The identity of the Font
object used to set the property, which is used solely by the Font
property getter
The text style, which the control will use to create its own private Font
object.
Because each Framework control will inherently clone any Font
instance used to set its Font
property, there's generally no need for user code to clone a font before using it to set another control's Font
property.