none
Get all styles for a specific font RRS feed

  • Question

  • Hi,

    I know it's an old question/problem flying around from time to time and a solution (WPF) was provided. Unfortunately, my knowledge WPF level is null - my kindly request is to have some hints/example about how to solve this problem using the classic Windows Form App (.NET). Means, having an installed family font with multiple styles (for example Bookman Old Style family has 4 styles: bold, bold italic, light and light italic) - what I want is to enumerate all this 4 fonts, their file names and then to use them (formatting some sample strings in a richtextbox) - summarizing, it's like to have access to the code behind a FontDialog Control

    Thanks in advance for any help

    Danny Costa

    Saturday, May 20, 2017 9:18 PM

All replies

  • Here is a basic start. You can add more style options etc.

    Imports System.Drawing.Text
    
    Public Class Form4
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim f As Font
            Dim FontList As New List(Of Font)
            Dim installed_fonts As New InstalledFontCollection
            Dim font_families() As FontFamily = installed_fonts.Families()
            For Each font_family As FontFamily In font_families
    
                If font_family.IsStyleAvailable(FontStyle.Regular) Then
                    f = New Font(font_family, 14, FontStyle.Regular)
                    FontList.Add(f)
                End If
    
                If font_family.IsStyleAvailable(FontStyle.Bold) Then
                    f = New Font(font_family, 14, FontStyle.Bold)
                    FontList.Add(f)
                End If
    
                If font_family.IsStyleAvailable(FontStyle.Italic) Then
                    f = New Font(font_family, 14, FontStyle.Italic)
                    FontList.Add(f)
                End If
    
            Next font_family
    
            For Each f In FontList
                RichTextBox1.SelectionFont = New Font(f.FontFamily, 12, f.Style)
                RichTextBox1.AppendText(f.FontFamily.Name & "   " & f.Style.ToString & vbLf)
            Next
    
        End Sub
    End Class

    • Proposed as answer by Frank L. Smith Saturday, May 20, 2017 10:16 PM
    Saturday, May 20, 2017 10:01 PM
  • Hello Tommy,

    I did not ask this time because I assumed your answer would be the next time. 

    I've put this on our website. 

    If you want me to remove it again than tell it please.

    http://www.vb-tips.com/FindFontStyles.ASPX


    Success
    Cor

    Sunday, May 21, 2017 8:56 AM
  • Hello Tommy,

    I did not ask this time because I assumed your answer would be the next time. 

    I've put this on our website. 

    If you want me to remove it again than tell it please.

    http://www.vb-tips.com/FindFontStyles.ASPX


    Success
    Cor

    That's fine Cor. You may want to mention the example only does 3 styles reg, bold, italic. One can add others like underline etc as desired.

    Sunday, May 21, 2017 1:46 PM

  • http://www.vb-tips.com/FindFontStyles.ASPX



    That's fine Cor. You may want to mention the example only does 3 styles reg, bold, italic. One can add others like underline etc as desired.

    done

    Success
    Cor

    Sunday, May 21, 2017 6:44 PM
  •  The easiest way to display all the font styles for a user to select such as the Light,  Light Italic,  Semibold,  Light Oblique,  Semibold Oblique,  and so on... is to just use a FontDialog.

     If that is not what you want to do,  then you are going to dive into actually iterating through all the installed fonts and either read the bytes of the font files to get the information you need,  or possibly get a LOGFONT structure of each font so that you can read the lfWeight member of the structure which would indicate those styles,as you can see in the msdn link i just gave to the LOGFONT structure. 

     The .Net methods as Tom has shown how to get will only let you get the Regular, Bold, Italic, Underlined,  and Strikeout styles.  It will not give you the others you seem to want.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, May 22, 2017 10:46 PM
    • Proposed as answer by Cor Ligthert Monday, May 22, 2017 11:08 PM
    Monday, May 22, 2017 8:45 PM