Thomas
Thomas

Reputation: 3

How to change font properties in VB.Net 2010

So I have been using this snippet of code to change font properties

Me.lblOutPut.Font = New Font("Times New Roman", 22)

However, I have options inside my program to change the font size and font type, and if I have to change the font size, for example, I would have to specify the font type as well in the code. Instead of going this route, is there a way to create variables which can dynamically change the font size and style?

Example, because I think I haven't made myself clear enough:

Me.lblOutPut.Font = New Font("Times New Roman", 16)

Me.lblOutPut.Font = New Font("Arial", 12)

How do I make it so that the font size does not change, but the font type does?

Upvotes: 0

Views: 6227

Answers (1)

shA.t
shA.t

Reputation: 16958

I think you need something like this:

Private Function setFont(myFont As Font, Optional fontFamily As String = "", Optional fontSize As Single = 0) As Font

    If fontFamily = "" Then
        fontFamily = myFont.FontFamily.ToString()
    End If

    If fontSize = 0 Then
        fontSize = myFont.Size
    End If

    Return New Font(fontFamily, fontSize)

End Function

That you can use it like this:

' To change both FontFamily And Size
Me.lblOutPut.Font = setFont(Me.lblOutPut.Font, "Times New Roman", 16)

' To change only FontFamily 
Me.lblOutPut.Font = setFont(Me.lblOutPut.Font, "Times New Roman", 0)
' Or
Me.lblOutPut.Font = setFont(Me.lblOutPut.Font, "Times New Roman")

' To change only Size
Me.lblOutPut.Font = setFont(Me.lblOutPut.Font, "", 12)

Upvotes: 1

Related Questions