Is it possible to change the size of a font in .net winforms without having to create a new Font with the new size?
A:
Make sure you use the constructor method that allows you to use the base font and pass in the new size you desire. This will save you some code from the other approaches.
Chris Porter
2009-05-28 19:36:39
I was thinking the same thing...but turns out that only works with _style_ not size :(
Paul Alexander
2009-05-28 19:43:05
Oops, you are correct. I remembered doing something like this but it was for creating a bold font from a non-bold font. Creating a different size font would require breaking out the necessary properties of the existing font.
Chris Porter
2009-05-29 16:54:52
+4
A:
You could do something like this with an Extension method.
Imports System.Runtime.CompilerServices Module FontExtensions <Extension()> Public Function ToSize(ByVal OriginalFont As Font, ByVal NewSize As Single) As Font Dim NewFont As Font NewFont = New Font(OriginalFont.FontFamily, NewSize, OriginalFont.Style) Return NewFont End Function End Module
and then call it like this...
SomeObject.Font = Font.ToSize(12)
It's still creating a new font behind the scenes, but your application code is not cluttered with the creation process.
Bill
2009-06-05 06:44:23