Results 1 to 8 of 8

Thread: [RESOLVED] ComboBox DrawItem...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Resolved [RESOLVED] ComboBox DrawItem...

    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

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: ComboBox DrawItem...

    Quote 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:
    1. Dim FontFamilies As New System.Drawing.Text.InstalledFontCollection()
    2. Dim Family As FontFamily
    3. ForEach Family In FontFamily.Families
    4. ComboBox1.Items.Add(Family.Name)
    5. Next

    Add this code into your Formload and your Combox will have all the Fonts installed on your Computer.

    Sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    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.

  4. #4
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: ComboBox DrawItem...

    Quote 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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [RESOLVED] ComboBox DrawItem...

    Sure Sparrow here it is (I'm still learning so it's not perfect )

    VB Code:
    1. Imports System.Drawing.Text
    2.  
    3. Public Class FontPractice
    4.  
    5.     'members
    6.     Public fntFamilyName As String
    7.     Public fnt As Font
    8.  
    9.     Private Sub FontPractice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         Dim installedFonts As New InstalledFontCollection()
    11.         Dim fntFamily As FontFamily
    12.         'go through the installed font collection and add each family to our combo box
    13.         For Each fntFamily In installedFonts.Families
    14.             fntFamilyName = fntFamily.Name
    15.             cboSelectFont.Items.Add(fntFamilyName)
    16.         Next
    17.     End Sub
    18.     'this function takes the font family we created and checks what style we can use
    19.     'it then returns the new font to be used
    20.     Public Function fntToUse(ByVal fontFamily) As Font
    21.         'check which styles are available for us to use with this font.
    22.         If fontFamily.IsStyleAvailable(FontStyle.Regular) Then
    23.             fntToUse = New Font(fntFamilyName, 10, FontStyle.Regular, GraphicsUnit.Point)
    24.         Else
    25.             If fontFamily.IsStyleAvailable(FontStyle.Bold) Then
    26.                 fntToUse = New Font(fntFamilyName, 10, FontStyle.Bold, GraphicsUnit.Point)
    27.             Else
    28.                 fntToUse = New Font(fntFamilyName, 10, FontStyle.Italic, GraphicsUnit.Point)
    29.             End If
    30.         End If
    31.         'return our font object
    32.         Return fntToUse
    33.     End Function
    34.  
    35.  
    36.     Private Sub cboSelectFont_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cboSelectFont.DrawItem
    37.         'get the graphics object
    38.         Dim grfx As Graphics = e.Graphics
    39.         Dim fontFamily As FontFamily
    40.  
    41.  
    42.         If e.Index > -1 Then
    43.             'get the font family name of the current item
    44.             fntFamilyName = cboSelectFont.Items(e.Index)
    45.             fontFamily = New FontFamily(fntFamilyName)
    46.             'call our function to check the styles of the font to make sure
    47.             'we don't try to use a style thats not supported
    48.             fnt = fntToUse(fontFamily)
    49.         End If
    50.         Try
    51.             'draw the item to the control, but catch any exceptions
    52.             grfx.DrawString(cboSelectFont.Items(e.Index), fnt, Brushes.Black, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
    53.         Catch ex As Exception
    54.         End Try
    55.  
    56.  
    57.     End Sub
    58.  
    59.     Private Sub cboSelectFont_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSelectFont.SelectedIndexChanged
    60.         Dim fnt As Font
    61.         Dim fontFamily As FontFamily
    62.         Dim fntName As String
    63.  
    64.         'get the name of the font family selected
    65.         fntName = cboSelectFont.SelectedItem.ToString()
    66.         'create an instance of the font family class with the font name
    67.         fontFamily = New FontFamily(fntName)
    68.         'pass the fontfamily to the function and get our font back
    69.         fnt = fntToUse(fontFamily)
    70.         lblSample.Font = fnt
    71.  
    72.     End Sub
    73. End Class

  6. #6
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [RESOLVED] ComboBox DrawItem...

    I do the same still learning!
    I'll tryed and let you know!

    Thanks,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [RESOLVED] ComboBox DrawItem...

    By the way, you need to set your combo box DrawMode to OwnerDrawFixed.

  8. #8
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [RESOLVED] ComboBox DrawItem...

    Quote 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.

    VB Code:
    1. cboSelectFont.Font = fnt

    just a thought.

    anuway thanks for your code,

    sparrow1
    Last edited by sparrow1; Mar 25th, 2006 at 01:05 PM.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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