Bind all fonts by name with their font style to the ComboBox using vb.net

How to fill combobox with installed fonts name and style

In this article we will bind all system’s fonts in a Combo box control, with the fonts displayed by name in that format. you can see below picture.

we have discussed earlier about how to bind all system font families name with their font in listview. see this. display-all-fonts-by-name-with-their-font-style-in-the-listview-using-vb-net
This code sample requires a ComboBox control named ‘ComboBox1′ on the windows form.

    Private Sub BindComboBox()
        ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
        ComboBox1.Font = New Font("Microsoft Sans Serif, 11.25pt", 11.25)
        ComboBox1.ItemHeight = 20
        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
            ComboBox1.Items.Add(tempFont)
        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.

At this time all font have been bind in the combobox1 control now we will redraw these items according to their font family with the help of
‘DrawItem’ event, so create a ComboBox1_DrawItem event and put the following code:

Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, _
                                   ByVal e As System.Windows.Forms.DrawItemEventArgs) _
                                   Handles ComboBox1.DrawItem
        e.DrawBackground()
        If (e.State And DrawItemState.Focus) <> 0 Then
            e.DrawFocusRectangle()
        End If
        Dim objBrush As Brush = Nothing
 
        Try
            objBrush = New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(DirectCast(ComboBox1.Items(e.Index), Font).Name, DirectCast(ComboBox1.Items(e.Index), Font), objBrush, e.Bounds)
        Finally
            If objBrush IsNot Nothing Then
                objBrush.Dispose()
            End If
            objBrush = Nothing
        End Try
    End Sub

try the above code and let me know about its functionality. One more thing we can create a user control with the help of ListBox and ComboBox control for targeting this functionality…..

Binding all font families name into the combobox

you can use this code sample for binding all font families to the combobox.

[Vb.net]

Private Sub BindFont()
        Dim AutoComStr As New AutoCompleteStringCollection()
        For Each fntfamily As FontFamily In FontFamily.Families
            comboBoxFontlist.Items.Add(fntfamily.Name)
            AutoComStr.Add(fntfamily.Name)
        Next
 
        comboBoxFontlist.AutoCompleteMode = AutoCompleteMode.Suggest
        comboBoxFontlist.AutoCompleteSource = AutoCompleteSource.CustomSource
        comboBoxFontlist.AutoCompleteCustomSource = AutoComStr
 End Sub

[C#]

private void BindFont()
    {
        AutoCompleteStringCollection AutoComStr = new AutoCompleteStringCollection();
        foreach (FontFamily fntfamily in FontFamily.Families)
        {
            comboBoxFontlist.Items.Add(fntfamily.Name);
            AutoComStr.Add(fntfamily.Name);
        }
 
        comboBoxFontlist.AutoCompleteMode = AutoCompleteMode.Suggest;
        comboBoxFontlist.AutoCompleteSource = AutoCompleteSource.CustomSource;
        comboBoxFontlist.AutoCompleteCustomSource = AutoComStr;
        }

3 thoughts on “Bind all fonts by name with their font style to the ComboBox using vb.net”

  1. thank you great work 
    But when choosing a font showing all the details
    font;14 pt,style=bold
    how to show just the font name without details?
     

  2. Hi qattosa

     

    you want to show only font name as a selected font. if you select ‘calibri’ from the dropdown then Combobox show only ‘calibri’ not ‘calibri font;14 pt,style=bold’…

     

    you need to work different from above. try this code:

    Private sub BindCombo()
    ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
            ComboBox1.Font = New Font(“Microsoft Sans Serif, 11.25pt”, 11.25)
            ComboBox1.ItemHeight = 20
            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
               ComboBox1.Items.Add(objFontFamily.Name)
            Next
    End sub

        Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
            e.DrawBackground()
            If (e.State And DrawItemState.Focus) <> 0 Then
                e.DrawFocusRectangle()
            End If
            Dim objBrush As Brush = Nothing

            Try
                objBrush = New SolidBrush(e.ForeColor)
                Dim _FontName As String = ComboBox1.Items(e.Index)
                Dim _font As Font
                Dim _fontfamily = New FontFamily(_FontName)
                If _fontfamily.IsStyleAvailable(FontStyle.Regular) Then
                    _font = New Font(_fontfamily, 14, FontStyle.Regular)
                ElseIf _fontfamily.IsStyleAvailable(FontStyle.Bold) Then
                    _font = New Font(_fontfamily, 14, FontStyle.Bold)
                ElseIf _fontfamily.IsStyleAvailable(FontStyle.Italic) Then
                    _font = New Font(_fontfamily, 14, FontStyle.Italic)
                End If

                e.Graphics.DrawString(_FontName, _font, objBrush, e.Bounds)
            Finally
                If objBrush IsNot Nothing Then
                    objBrush.Dispose()
                End If
                objBrush = Nothing
            End Try

        End Sub

Comments are closed.