I have some user controls in a windows form. I wonder
if i set the Font
property of the main form, will its child get a) a copy of the new Font
, or b) a reference to the new Font
, or c) nothing?
Does a font need to be disposed? For example, can I do the following code safely?
form.Font = new Font(...);
Will a font get disposed automatically when the parent (Form
or UserControl
) is disposed?
Thanks,
Gilbert
From what I can tell, the Font property of a control is used to determine the font settings to use when drawing the control, but the GDI Font associated with that property isn't used to do the drawing. A control won't care if the Font that's assigned to its Font property is disposed after it's assigned, or even before it's assigned. The control is clearly capable of using some hidden aspects of the Font object which are available even after it is Disposed to determine the appropriate attributes of the font, but I don't know whether it
- Uses the supplied Font object, if not disposed, or else creates its own temporary font object each time it has to draw something and disposes the temporary object immediately thereafter.
- Uses the supplied Font object, if not disposed, or else creates its own private font object which it will dispose of when either the Font property is reassigned or the control is disposed.
- Copies the font family, size, etc. from the Font object when the Font property is assigned, and does everything with its private temporary or persistent Font objects it creates itself.
- Copies the font family, size, etc. from the assigned Font object whenever it's necessary to draw something.
The control certainly keeps a reference to the passed-in font object, if for no other reason than to supply it to the Font property getter. I have no idea, though, whether it's better to dispose the Font after assigning it, or whether it's better to keep a copy of the Font in the form and dispose it when the Form itself is disposed.