Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font is installed? function

I can improve my function to not search by each element?

#Region " Font Is Installed? Function "

    ' [ Font Is Installed? Function ]
    '
    ' Examples :
    ' MsgBox(Font_Is_Installed("Lucida Console"))

    Private Function Font_Is_Installed(ByVal FontName As String) As Boolean
        Dim AllFonts As New Drawing.Text.InstalledFontCollection
        For Each Font As FontFamily In AllFonts.Families
            If Font.Name.ToLower = FontName.ToLower Then Return True
        Next
        Return False
    End Function

#End Region

UPDATE:

Ok now I saw the ".tolist" function and now my code is like this:

Private Function Font_Is_Installed(ByVal FontName As String) As Boolean
    Dim AllFonts As New Drawing.Text.InstalledFontCollection
    Dim FontFamily As New FontFamily(FontName)
    If AllFonts.Families.ToList().Contains(FontFamily) Then Return True Else Return False
End Function

I have the same question: Is best improved by the second way, or I can improve it better?

like image 288
ElektroStudios Avatar asked Sep 01 '26 08:09

ElektroStudios


2 Answers

here IS

    Public Shared Function IsFontInstalled(ByVal FontName As String) As Boolean
        Using TestFont As Font = New Font(FontName, 10)
            Return CBool(String.Compare(FontName, TestFont.Name, StringComparison.InvariantCultureIgnoreCase) = 0)
        End Using
    End Function
like image 79
Jack Gajanan Avatar answered Sep 03 '26 23:09

Jack Gajanan


    Dim SomeTextBox As TextBox = New TextBox()
    Dim SomeFontFamily As FontFamily = Nothing
    Dim SomeFontCollection As PrivateFontCollection = Nothing

    Try
        SomeFontFamily = New FontFamily("SomeFontFamilyName")
    Catch ex As Exception
        SomeFontCollection = New PrivateFontCollection()
        SomeFontCollection.AddFontFile("SomeFontFileName")
        SomeFontFamily = SomeFontCollection.Families(0)
    End Try

    SomeTextBox.Font = New Font(SomeFontFamily, 12)

This way SomeFontFamily will only be created from file if it cannot be created from a local font. SomeTextBox will display the proper font.

like image 27
Hank van de Kam Avatar answered Sep 03 '26 23:09

Hank van de Kam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!