In this article we create a function in c# language that bind all system’s fonts in a Listview
control, with the fonts displayed by name in that format. you can see below picture.
This code sample requires a Listview
control named ‘ListView1’ on the windows form.
C# Code
private void BindListView() { FontFamily objFontFamily = default(FontFamily); System.Drawing.Text.FontCollection objFontCollection = null; Font tempFont = default(Font); objFontCollection = new System.Drawing.Text.InstalledFontCollection(); foreach ( objFontFamily in objFontCollection.Families) { if (objFontFamily.IsStyleAvailable(FontStyle.Regular)) { tempFont = new Font(objFontFamily, 14, FontStyle.Regular); } else if (objFontFamily.IsStyleAvailable(FontStyle.Bold)) { tempFont = new Font(objFontFamily, 14, FontStyle.Bold); } else if (objFontFamily.IsStyleAvailable(FontStyle.Italic)) { tempFont = new Font(objFontFamily, 14, FontStyle.Italic); } ListViewItem lst = new ListViewItem(); lst.Font = tempFont; lst.Text = tempFont.FontFamily.Name; ListView1.Items.Add(lst); } }
VB.Net code
Private Sub BindListView() Dim objFontFamily As FontFamily Dim objFontCollection As System.Drawing.Text.FontCollection Dim tempFont As Font objFontCollection = New System.Drawing.Text.InstalledFontCollection() For Each objFontFamily In objFontCollection.Families If objFontFamily.IsStyleAvailable(FontStyle.Regular) Then tempFont = New Font(objFontFamily, 14, FontStyle.Regular) ElseIf objFontFamily.IsStyleAvailable(FontStyle.Bold) Then tempFont = New Font(objFontFamily, 14, FontStyle.Bold) ElseIf objFontFamily.IsStyleAvailable(FontStyle.Italic) Then tempFont = New Font(objFontFamily, 14, FontStyle.Italic) End If Dim lst As New ListViewItem lst.Font = tempFont lst.Text = tempFont.FontFamily.Name ListView1.Items.Add(lst) Next End Sub
you can see that we get all system fonts by using InstalledFontCollection()
method in System.Drawing.Text. This returns a System.Drawing.Text.FontCollection
object. and then we find each fontfamily from FontCollection
object.
Bind only available font names in the listbox
Following example code shows how to get all installed font families in vb.net. The example requires a ListBox
control named ListBox1
on your windows form.
Imports System.Drawing.Text Private Sub BindFontsInList() Dim objFontCollection As New InstalledFontCollection ' Get An Array Of The System's Font Familiies. Dim objFontFamily() As FontFamily = objFontCollection.Families() ' Display The Font Families. For Each Font As FontFamily In objFontFamily ListBox1.Items.Add(Font.Name) Next End Sub