|
-
Mar 25th, 2006, 06:07 AM
#1
Thread Starter
Hyperactive Member
-
Mar 25th, 2006, 07:55 AM
#2
Re: ComboBox DrawItem...
 Originally Posted by Tinbeard
I'm struggleing a bit with this one, I'm just messing about trying to broaden my knowledge and one of the things I'm trying to do is work out the custom drawing of controls.
If I add an item to the combo box I'd expect the DrawItem event to be fired then but it doesn't seem to be the case.
For example if I say want to populate a combo box with the fonts I have installed on my system I setup a For Each loop to go through the collection and then add the font family to the combo box 'cbInstalledFonts.Items.Add(fntFamily)' , at this point I'd expect the DrawItem event to fire but the loop just continues
When exaclty is the drawItem event called ? On MSDN it really doesn't make it clear, well to me anyway 
Hi,
You could try this code to retrieve a list of all the fonts on the current computer. And use the InstalledFontCollection Class.
VB Code:
Dim FontFamilies As New System.Drawing.Text.InstalledFontCollection()
Dim Family As FontFamily
ForEach Family In FontFamily.Families
ComboBox1.Items.Add(Family.Name)
Next
Add this code into your Formload and your Combox will have all the Fonts installed on your Computer.
Sparrow1
-
Mar 25th, 2006, 09:09 AM
#3
Thread Starter
Hyperactive Member
Re: ComboBox DrawItem...
Thanks Sparrow, I'm ok with populating the combo box with the fonts installed.
What I was trying to do is control the drawitem event so I could draw each font name in it's own font.
I've got this figured out now, it was just a matter of getting the code within the drawitem event right, I'd got my self confused about when this event is triggered and as such the code I wrote to handle it was wrong.
-
Mar 25th, 2006, 09:22 AM
#4
Re: ComboBox DrawItem...
 Originally Posted by Tinbeard
Thanks Sparrow, I'm ok with populating the combo box with the fonts installed.
What I was trying to do is control the drawitem event so I could draw each font name in it's own font.
I've got this figured out now, it was just a matter of getting the code within the drawitem event right, I'd got my self confused about when this event is
triggered and as such the code I wrote to handle it was wrong.
I'm interested in that too, can you let me see that specificcode.
Thanks in advance,
sparrow1
-
Mar 25th, 2006, 09:44 AM
#5
Thread Starter
Hyperactive Member
Re: [RESOLVED] ComboBox DrawItem...
Sure Sparrow here it is (I'm still learning so it's not perfect )
VB Code:
Imports System.Drawing.Text
Public Class FontPractice
'members
Public fntFamilyName As String
Public fnt As Font
Private Sub FontPractice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim installedFonts As New InstalledFontCollection()
Dim fntFamily As FontFamily
'go through the installed font collection and add each family to our combo box
For Each fntFamily In installedFonts.Families
fntFamilyName = fntFamily.Name
cboSelectFont.Items.Add(fntFamilyName)
Next
End Sub
'this function takes the font family we created and checks what style we can use
'it then returns the new font to be used
Public Function fntToUse(ByVal fontFamily) As Font
'check which styles are available for us to use with this font.
If fontFamily.IsStyleAvailable(FontStyle.Regular) Then
fntToUse = New Font(fntFamilyName, 10, FontStyle.Regular, GraphicsUnit.Point)
Else
If fontFamily.IsStyleAvailable(FontStyle.Bold) Then
fntToUse = New Font(fntFamilyName, 10, FontStyle.Bold, GraphicsUnit.Point)
Else
fntToUse = New Font(fntFamilyName, 10, FontStyle.Italic, GraphicsUnit.Point)
End If
End If
'return our font object
Return fntToUse
End Function
Private Sub cboSelectFont_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cboSelectFont.DrawItem
'get the graphics object
Dim grfx As Graphics = e.Graphics
Dim fontFamily As FontFamily
If e.Index > -1 Then
'get the font family name of the current item
fntFamilyName = cboSelectFont.Items(e.Index)
fontFamily = New FontFamily(fntFamilyName)
'call our function to check the styles of the font to make sure
'we don't try to use a style thats not supported
fnt = fntToUse(fontFamily)
End If
Try
'draw the item to the control, but catch any exceptions
grfx.DrawString(cboSelectFont.Items(e.Index), fnt, Brushes.Black, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
Catch ex As Exception
End Try
End Sub
Private Sub cboSelectFont_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSelectFont.SelectedIndexChanged
Dim fnt As Font
Dim fontFamily As FontFamily
Dim fntName As String
'get the name of the font family selected
fntName = cboSelectFont.SelectedItem.ToString()
'create an instance of the font family class with the font name
fontFamily = New FontFamily(fntName)
'pass the fontfamily to the function and get our font back
fnt = fntToUse(fontFamily)
lblSample.Font = fnt
End Sub
End Class
-
Mar 25th, 2006, 10:18 AM
#6
Re: [RESOLVED] ComboBox DrawItem...
I do the same still learning!
I'll tryed and let you know!
Thanks,
sparrow1
-
Mar 25th, 2006, 11:41 AM
#7
Thread Starter
Hyperactive Member
Re: [RESOLVED] ComboBox DrawItem...
By the way, you need to set your combo box DrawMode to OwnerDrawFixed.
-
Mar 25th, 2006, 12:50 PM
#8
Re: [RESOLVED] ComboBox DrawItem...
 Originally Posted by Tinbeard
By the way, you need to set your combo box DrawMode to OwnerDrawFixed.
That was the thing I needed, because It gave me always an Error.
Now everything is working fine!
Found something more!
If you select a Font in the ComboBox, then just Add this code after lblSample.Font = fnt and the selected Font will be shown in your ComboBox.
just a thought.
anuway thanks for your code,
sparrow1
Last edited by sparrow1; Mar 25th, 2006 at 01:05 PM.
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
|