views:

331

answers:

1

Hi, I'm developing a Windows Forms Application in Visual Basic .NET with Visual Studio 2008.

I'm trying to compose fonts (Family name, font size, and the styles) at runtime, based on user preferences, and apply them to labels.

For the sake of both a simplier user interface, and compatibility between more than one machine requiring to use the same font, I'll NOT use the InstalledFontCollection, but a set of buttons that will set few selected fonts, that I know to be present in all machines (fonts like Verdana).

So, I have to make a Public Sub on a Module that will create fonts, but I don't know how to code that. There are also four CheckBoxes that set the styles, Bold, Italic, Underline and Strikeout.

How should I code this? The SomeLabel.Font.Bold property is readonly, and there seems to be a problem when converting a string like "Times New Roman" to a FontFamily type. (It just says it could not do it)

Like on

Dim NewFontFamily As FontFamily = "Times New Roman"

Thanks in advance.

+1  A: 

This should resolve your font issue:

Label1.Font = New Drawing.Font("Times New Roman", _
                               16,  _
                               FontStyle.Bold or FontStyle.Italic)

MSDN documention on Font property here

A possible implementation for the funtion that creates this font might look like this:

Public Function CreateFont(ByVal fontName As String, _
                           ByVal fontSize As Integer, _
                           ByVal isBold As Boolean, _
                           ByVal isItalic As Boolean, _
                           ByVal isStrikeout As Boolean) As Drawing.Font

    Dim styles As FontStyle = FontStyle.Regular

    If (isBold) Then
        styles = styles Or FontStyle.Bold
    End If

    If (isItalic) Then
        styles = styles Or FontStyle.Italic
    End If

    If (isStrikeout) Then
        styles = styles Or FontStyle.Strikeout
    End If

    Dim newFont As New Drawing.Font(fontName, fontSize, styles)
    Return newFont

End Function

Fonts are immutable, that means once they are created they cannot be updated. Therefore all the read-only properties that you have noticed.

Philip Fourie
Thanks! =) It solved my issue!
Camilo Martin
And just in case it may help others, what I did was to use the very information from the previous font to make the next one, except for the value I wanted changed.In my opinion, it would have been easier if VB.NET just did all the repetitive coding when I ask a single property to be changed, but it's quite easy already so I better not complain! =P
Camilo Martin