Results 1 to 9 of 9

Thread: [RESOLVED] List available fonts in a combo box?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    [RESOLVED] List available fonts in a combo box?

    I am trying to create a combo box that list the available fonts on the users system. I found what I want on the link below, but it is written in C and I do not know how to convert it. Does anyone have an idea how to do this or how to convert the code below?

    Thanks


    Click here
    Last edited by birthjay; Jan 9th, 2004 at 12:12 PM.

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    VB Code:
    1. For Each fnt as System.Drawing.FontFamily In System.Drawing.FontFamily.Families
    2.     Combobox1.Items.Add(fnt.Name)
    3. Next
    Though it's just a simple combo, not like those in that page.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Thanks!

    That works great. Thanks!

    I would like to know how to do the others though. I am taking a C# course beginning in a few days. Maybe I will work through it then.

    Thanks again!

  4. #4
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Add a combobox named cmbFonts to a form
    Set the DrawMode property to OwnerDrawFixed
    in the form_load event call the LoadFonts sub.

    I think that's what you're looking for.

    VB Code:
    1. Private Sub LoadFonts()
    2.         Dim Fs() As FontFamily = FontFamily.Families
    3.         Dim F As FontFamily
    4.  
    5.         cmbFonts.Items.Clear()
    6.  
    7.         For Each F In Fs
    8.             cmbFonts.Items.Add(F.Name)
    9.         Next
    10.  
    11.         cmbFonts.SelectedIndex = 0
    12.  
    13.     End Sub
    14.     Private Sub cmbFonts_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cmbFonts.DrawItem
    15.         Dim S As String = cmbFonts.Items(e.Index)
    16.         Dim fam As New FontFamily(S)
    17.         Dim F As Font
    18.         Dim sf As New StringFormat()
    19.         sf.Alignment = StringAlignment.Near
    20.         sf.LineAlignment = StringAlignment.Center
    21.  
    22.         If fam.IsStyleAvailable(FontStyle.Regular) = True Then
    23.             F = New Font(S, 12, FontStyle.Regular)
    24.         ElseIf fam.IsStyleAvailable(FontStyle.Bold) = True Then
    25.             F = New Font(S, 12, FontStyle.Bold)
    26.         ElseIf fam.IsStyleAvailable(FontStyle.Italic) = True Then
    27.             F = New Font(S, 12, FontStyle.Italic)
    28.         Else
    29.             F = cmbFonts.Font
    30.         End If
    31.  
    32.         e.DrawBackground()
    33.         e.DrawFocusRectangle()
    34.  
    35.         e.Graphics.DrawString(S, F, New SolidBrush(e.ForeColor), New RectangleF(e.Bounds.Left + 1, e.Bounds.Top + 1, e.Bounds.Width - 2, e.Bounds.Height - 2), sf)
    36.  
    37.         S = Nothing
    38.         F = Nothing
    39.         fam = Nothing
    40.  
    41.     End Sub
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Error

    Dim S As String = cmbFonts.Items(e.Index)


    Thanks, but I am getting an error on the above line. It tells me that "Option Strict On disallows implicit conversions from 'System.object' to 'String'". What's do I need to change? Also, what is this going to produce?

    Thanks!

  6. #6
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    VB Code:
    1. Dim S As String = Convert.ToString(cmbFonts.Items(e.Index))
    Try that?

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Thanks!

    Perfect! Thanks folks!

    That works great and I have tried to figure the next thing out, but it is late and I am getting nowhere.

    I basically now want to set the selected font from the combo box to a label. It sounds simple, but it ain't working for me. I feel guilty asking, but can you point me in the right direction on that as well?

    Thanks!
    Last edited by birthjay; Jan 9th, 2004 at 01:04 AM.

  8. #8
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    on the SelectedIndexChanged event of the combobox try this:

    Label1.Font=New Font(cmbFonts.Text,FontStyle.Regular)

    (or something like that )
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Thanks!

    Got it! Thanks!

    Something interesing I found out about the DropDownStyle:

    If you select "DropDown" then the combo box text is plain while the available fonts are displayed as the actually look, but if you select "DropDownList" then the combo box text looks just like the actual font that is selected.

    Thanks again for all the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width