Results 1 to 22 of 22

Thread: bitmaps or icons in combobox?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31

    bitmaps or icons in combobox?

    How does one (if at all possible) display a unique bitmap or icon beside the text for each item in a combobox?

    The VB .Net combobox supports the BackgroundImage and Image properties.

    Many thanks in advance

  2. #2

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31
    Thanks, I'll check them out

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is a VB.NET translation of the C# class. It works pretty good. Its easiest if you just draw a normal combo on the form and then change in auto generated code from the ComboBox to ComboBoxEx.

    VB Code:
    1. Public Class ComboBoxEx
    2.     Inherits ComboBox
    3.  
    4.     Private _imageList As ImageList
    5.  
    6.     Public Property ImageList() As imageList
    7.         Get
    8.             Return _imageList
    9.         End Get
    10.         Set(ByVal value As imageList)
    11.             _imageList = value
    12.         End Set
    13.     End Property
    14.  
    15.     Public Sub New()
    16.         DrawMode = DrawMode.OwnerDrawFixed
    17.     End Sub
    18.  
    19.     Protected Overrides Sub OnDrawItem(ByVal ea As DrawItemEventArgs)
    20.         ea.DrawBackground()
    21.         ea.DrawFocusRectangle()
    22.  
    23.         Dim item As ComboBoxExItem
    24.         Dim imageSize As Size = ImageList.ImageSize
    25.         Dim bounds As Rectangle = ea.Bounds
    26.  
    27.         Try
    28.             item = CType(Items(ea.Index), ComboBoxExItem)
    29.  
    30.             If item.ImageIndex <> -1 Then
    31.                 ImageList.Draw(ea.Graphics, bounds.Left, bounds.Top, item.ImageIndex)
    32.                 ea.Graphics.DrawString(item.Text, ea.Font, New SolidBrush(ea.ForeColor), bounds.Left + imageSize.Width, bounds.Top)
    33.             Else
    34.                 ea.Graphics.DrawString(item.Text, ea.Font, New SolidBrush(ea.ForeColor), bounds.Left, bounds.Top)
    35.             End If
    36.         Catch e As Exception
    37.             If ea.Index <> -1 Then
    38.                 ea.Graphics.DrawString(Items(ea.Index).ToString(), ea.Font, New SolidBrush(ea.ForeColor), bounds.Left, bounds.Top)
    39.             Else
    40.                 ea.Graphics.DrawString(Text, ea.Font, New SolidBrush(ea.ForeColor), bounds.Left, bounds.Top)
    41.             End If
    42.       finally
    43.  
    44.             MyBase.OnDrawItem(ea)
    45.         End Try
    46.     End Sub
    47.  
    48.     Public Class ComboBoxExItem
    49.         Private _text As String
    50.         Private _imageIndex As Integer = -1
    51.  
    52.         Public Property Text() As String
    53.             Get
    54.                 Return _text
    55.             End Get
    56.             Set(ByVal Value As String)
    57.                 _text = Value
    58.             End Set
    59.         End Property
    60.  
    61.         Public Property ImageIndex() As Integer
    62.             Get
    63.                 Return _imageIndex
    64.             End Get
    65.             Set(ByVal Value As Integer)
    66.                 _imageIndex = Value
    67.             End Set
    68.         End Property
    69.  
    70.         Public Sub New()
    71.             MyBase.new()
    72.         End Sub
    73.  
    74.         Public Sub New(ByVal text As String)
    75.             MyBase.new()
    76.             Me.Text = text
    77.         End Sub
    78.  
    79.         Public Sub New(ByVal text As String, ByVal imageIndex As Integer)
    80.             _text = text
    81.             _imageIndex = imageIndex
    82.         End Sub
    83.  
    84.         Public Overrides Function ToString() As String
    85.             Return _text
    86.         End Function
    87.  
    88.     End Class
    89. End Class

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31
    Being the absolute moron I am, I can't figure out how to properly implement the example. I changed all of the control references in code and the error when I try to compile is:
    "Reference to a non-shared member requires an object reference"

    Here is the code I'm using:

    Dim MyList As New ArrayList()
    MyList.Add(New GenericList("Jack Hammer", 1))
    MyList.Add(New GenericList("Sledge Hammer", 2))
    MyList.Add(New GenericList("Screwdriver", 3))
    MyList.Add(New GenericList("Wrench", 4))

    'bind list
    ComboBoxEx.DataSource = MyList
    ComboBoxEx.DisplayMember = "Display"
    ComboBoxEx.ValueMember = "ID"

    As you can see, I'm trying to combine both examples you posted.

    Thanks again for your expert advice on this, have a great weekend

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well unforunately that ComboBox Extender class isn't made to take other items with images except the ComboBoxExItem class. You can still use binding but you couln'd have the Items be of the type GenericList. Although You could have another class either implement a common interface or inherit from the ComboBoxExItem class and do it that way. But when the ComboBoxEx overrides the OnPaint event of the normal ComboBox it uses the ImageIndex and Text properties to display the item with a picture. Or you could go through and devise some type of ImageIndexMember type property for it.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you want to use the ComboBoxExItems then you can do this:
    VB Code:
    1. Dim al As New ArrayList()
    2.         al.Add(New ComboBoxEx.ComboBoxExItem("Test1", 1))
    3.         al.Add(New ComboBoxEx.ComboBoxExItem("Test2", 0))
    4.         al.Add(New ComboBoxEx.ComboBoxExItem("Test3", 1))
    5.         al.Add(New ComboBoxEx.ComboBoxExItem("Test4", 0))
    6.         al.Add(New ComboBoxEx.ComboBoxExItem("Test5", 1))
    7.  
    8.         ComboBox1.ImageList = ImageList1
    9.         ComboBox1.DataSource = al
    10.         ComboBox1.DisplayMember = "Text"
    11.         ComboBox1.ValueMember = "ImageIndex"

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is a version that can display an image for anything that implements ImageComboBox.IImageComboItem and a sample of the genericlist class extended to implement the interface.

    Although I think it'd be better to Just workin a ImageMember proeprty so I'm going to try that next.

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Actually this one is much better because you don't need to implement anything just set the imageindexmemeber property of the ImageComboBox and you are good to go.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31

    Cool Excellent ...

    That is a perfect example Can't believe it's actually that simple!

    Once again, thanks a million for your help, I wish Microsoft provided example code like that.

    Best regards,
    George

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31

    Question ??

    When I add your classes into my project everything is fine except for one small problem:

    Me.ComboBox1 = New ImageComboBinding.ImageComboBox()
    it underlines the above line, and I can't find any other reference for "ImageComboBinding" anywhere in the project. Help!!

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31

    Lightbulb Whoops ...

    Figured it out, ImageComboBinding is the name of the "solution". Hehehe, sorry about that.

    Once again, thanks for your help

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    ImageComboBinding was the namespace of the example project, so if you moved the classes over then there is no more ImageComboBinding namespace. Also I added a constructor that takes a standard combo and converts it to the ImageCombo but I'm not sure if it was in that version. That way you don't have to mess with any autogenerated code (which sometimes gets rewritten) you just add a normal code to the form set it up and then change it in the form load event. So if this doesn't work:
    VB Code:
    1. Dim icbo As ImageComboBox
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         'ini the imagecombo
    5.         icbo = New ImageComboBox(ComboBox1, Me)
    Then let me know and I'll load the newer version.

    Sorry I was typing while you were posting.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31

    Question

    icbo = New ImageComboBox(ComboBox1, Me)

    Please do post the latest, it generates a compiler error of:
    "too many arguments to public sub New()


    Thanks in advance,
    George

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here you go.

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31

    Talking Yeah!

    Works like a charm! Thanks again. I hope others are tuning into this thread.

    Best regards,
    George

    p.s. I havn't cursed at Microsoft for a few days now

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31

    Unhappy One small detail...

    I have run into the problem that if the ComboBox is on a tabpage instead of on the main form, then it won't populate with any items unless I put the combobox on the main form. Should I create a combobox class for the tabsheet itself? Any suggestions or help you can give me on this would be greatly appreciated.

    Thanks in advance

  18. #18
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    First try changing the Auto Generated text instead of using Passing in a normal combo to the constructor to create it. The code in there doesn't grab all the properties from the normal combo and that may be it. I tried to get it to loop through the properties but that had a few bugs so try that first.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31

    Talking You Da' man!

    That solved the problem. I just scares me to change the auto-generated stuff because at one point all of the controls vanished from the form I was working on (although it didn't do that this time). Thanks again Edneeis!

    Have a great weekend

  20. #20
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you figure out what properties can and can't be set then we can fix the constructor way. Its just when I tried to set them all the list wouldn't appear then. So I just tried to manually set all the properties I thought would be needed. Hmmm, actually I just realized something that may fix it....

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31

    Cool Away for the weekend

    I'll catch up with you on Monday, have a good one, and thanks again for the help on this

  22. #22
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Sorry fixed it and forgot to post it.

    Replace this constructor:
    Public Sub New(ByRef cbo As ComboBox, frm as Form)

    With this instead:
    VB Code:
    1. Public Sub New(ByRef cbo As ComboBox)
    2.         'assign all properties from cbo to me
    3.         'Dim pi As Reflection.PropertyInfo
    4.         'For Each pi In cbo.GetType.GetProperties
    5.         '    Dim s As String = pi.Attributes.ToString
    6.         '    If pi.CanWrite Then
    7.         '        'On Error Resume Next 'just in case
    8.         '        Me.GetType.GetProperty(pi.Name).SetValue(Me, pi.GetValue(cbo, Nothing), Nothing)
    9.         '    End If
    10.         'Next
    11.         'TODO: have it consume ALL properties of original combo
    12.         Me.Anchor = cbo.Anchor
    13.         Me.BackColor = cbo.BackColor
    14.         Me.BackgroundImage = cbo.BackgroundImage
    15.         Me.CausesValidation = cbo.CausesValidation
    16.         Me.ContextMenu = cbo.ContextMenu
    17.         Me.DataSource = cbo.DataSource
    18.         Me.DisplayMember = cbo.DisplayMember
    19.         Me.Dock = cbo.Dock
    20.         Me.DropDownStyle = cbo.DropDownStyle
    21.         Me.DropDownWidth = cbo.DropDownWidth
    22.         Me.Enabled = cbo.Enabled
    23.         Me.Font = cbo.Font
    24.         Me.ForeColor = cbo.ForeColor
    25.         Me.IntegralHeight = cbo.IntegralHeight
    26.         If cbo.Items.Count > 0 Then
    27.             Dim tmp(cbo.Items.Count) As Object
    28.             cbo.Items.CopyTo(tmp, 0)
    29.             Me.Items.AddRange(tmp)
    30.         End If
    31.         Me.MaxDropDownItems = cbo.MaxDropDownItems
    32.         Me.MaxLength = cbo.MaxLength
    33.         Me.Sorted = cbo.Sorted
    34.         Me.Text = cbo.Text
    35.         Me.TabStop = cbo.TabStop
    36.         Me.ValueMember = cbo.ValueMember
    37.         Me.Visible = cbo.Visible
    38.         Me.Location = cbo.Location
    39.         Me.Size = cbo.Size
    40.         Me.TabIndex = cbo.TabIndex
    41.         'set to ownerdraw
    42.         Me.DrawMode = DrawMode.OwnerDrawFixed
    43.         'switch combos
    44.         Dim parent As Object = cbo.Parent
    45.         parent.Controls.Remove(cbo)
    46.         parent.Controls.Add(Me)
    47.     End Sub

    Then just pass in the combo it is replacing instead of the combo and form.

    icbo = New ImageComboBox(ComboBox1)

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