fedeteka fedeteka - 1 year ago 82
Vb.net Question

More easy way to show if a Font is installed or not

I'm using this code and works fine

Imports System.Drawing.Text

Dim fontInstalled As Boolean = False
Dim fontToSearch As String
Dim fonts As New InstalledFontCollection

fontToSearch = "Verdana"

For Each one As FontFamily In fonts.Families
'If I want to show every installed family name
If one.Name = fontToSearch Then
fontInstalled = True
MsgBox("Font " & fontToSearch & " IS installed!!!")
Exit For
End If

If fontInstalled = False Then MsgBox("Font " & fontToSearch & " is NOT installed")

But I'm sure there will be a cleaner solution using InstalledFontCollection or something but I can't adapt this code to VB.NET Test if a Font is installed

var fontsCollection = new InstalledFontCollection();
foreach (var fontFamiliy in fontsCollection.Families)
if (fontFamiliy.Name == fontName) ... \\ installed

Answer Source

Whenever you use the keyword New , check to see if it includes a Dispose method. If it does, it means that it likely allocates some resource which needs to be Disposed. So, use it in a Using block

1. Check if installed

Dim IsInstalled As Boolean

Using fnts As New InstalledFontCollection()
    IsInstalled = fnts.Families.
                Select(Function(s) s.Name).
End Using

2. Try It

Using fnt As New Font("Verdana", 12)
    IsInstalled = (fnt IsNot Nothing)
End Using

3. Get List of Installed Names

Dim fontNames As String()
Using fnts As New InstalledFontCollection()
    fontNames = fnts.Families.
                Select(Function(s) s.Name).
End Using

Bonus Tip

MsgBox / MessageBox is a horrible way to debug. From the menu select Debug | Windows | Output. You can dock this where ever you want and set it to "Auto Hide" so it hides unless the mouse is over it. To output to it:

For Each s As String In fontNames

et voila no more clicking to dispose of 172 font name dialogs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download