|
-
Jan 7th, 2004, 10:28 PM
#1
Thread Starter
Fanatic Member
[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.
-
Jan 7th, 2004, 10:45 PM
#2
Frenzied Member
VB Code:
For Each fnt as System.Drawing.FontFamily In System.Drawing.FontFamily.Families
Combobox1.Items.Add(fnt.Name)
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
-
Jan 8th, 2004, 08:28 AM
#3
Thread Starter
Fanatic Member
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!
-
Jan 8th, 2004, 09:10 AM
#4
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:
Private Sub LoadFonts()
Dim Fs() As FontFamily = FontFamily.Families
Dim F As FontFamily
cmbFonts.Items.Clear()
For Each F In Fs
cmbFonts.Items.Add(F.Name)
Next
cmbFonts.SelectedIndex = 0
End Sub
Private Sub cmbFonts_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cmbFonts.DrawItem
Dim S As String = cmbFonts.Items(e.Index)
Dim fam As New FontFamily(S)
Dim F As Font
Dim sf As New StringFormat()
sf.Alignment = StringAlignment.Near
sf.LineAlignment = StringAlignment.Center
If fam.IsStyleAvailable(FontStyle.Regular) = True Then
F = New Font(S, 12, FontStyle.Regular)
ElseIf fam.IsStyleAvailable(FontStyle.Bold) = True Then
F = New Font(S, 12, FontStyle.Bold)
ElseIf fam.IsStyleAvailable(FontStyle.Italic) = True Then
F = New Font(S, 12, FontStyle.Italic)
Else
F = cmbFonts.Font
End If
e.DrawBackground()
e.DrawFocusRectangle()
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)
S = Nothing
F = Nothing
fam = Nothing
End Sub
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jan 8th, 2004, 10:24 PM
#5
Thread Starter
Fanatic Member
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!
-
Jan 8th, 2004, 10:55 PM
#6
VB Code:
Dim S As String = Convert.ToString(cmbFonts.Items(e.Index))
Try that?
-
Jan 9th, 2004, 12:04 AM
#7
Thread Starter
Fanatic Member
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.
-
Jan 9th, 2004, 02:07 AM
#8
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...
-
Jan 9th, 2004, 12:11 PM
#9
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|