views:

69

answers:

2

We all know that the Control base class has a Scale method. The problem is this: it's absolute crap. While it admirably scales all of the controls, it does not scale the fonts. So all my my labels, text boxes, and buttons look so very awkward. It would be so simple to just loops through all of the form's controls and change the font, but Control.Font.Size is read-only so foreach(Control C in this.Controls){C.Font.Size = C.Font.Size * .8f;} won't do the trick.

What is the best way to scale the font of every control in a form when the form itself is scaled?

+1  A: 

To change the font size of a control, you have to create a new Font object and assign it to the control.

You can use the Font object in the existing control as a template to create the new Font object, while changing only the font properties you want, thusly:

myControl.Font = new Font(myControl.Font, 
    myControl.Font.Style | FontStyle.Bold);

There's also a font scaling tutorial with some sample code here: http://www.switchonthecode.com/tutorials/csharp-tutorial-font-scaling

Robert Harvey
Nice find, thanks!
Steve H.
A: 

Font scaling is not that easy, because you cannot control the height and width, you have only one property: Size.

What I normally do is use the TableLayoutPanel intensively. You can then set the font Size, and all controls should (if used Docking and AutoSize properly) scale nicely.

Oh, and change the font size by setting a the Font property of the Form to a new Font(...)

GvS