Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change font size without creating a new font

Is it possible to change the size of a font in .net winforms without having to create a new Font with the new size?

like image 926
jvanderh Avatar asked Dec 23 '22 11:12

jvanderh


1 Answers

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.

like image 184
Bill Avatar answered Jan 29 '23 23:01

Bill