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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With