Results 1 to 38 of 38

Thread: ListBox with custom items (colors, images, text alignment)

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    ListBox with custom items (colors, images, text alignment)

    Hey,

    Here is a very simple owner-drawn ListBox control that allows you to specify a Color and Image for each item. It uses a very simple method to only allow items of a specific type (and not all Objects as usual) which is easily extended.



    I know you could just use a ListView for this, but I came across this method when creating a different control (a 'large item' listbox that looks like the Downloads window in FireFox) and decided to share it, because it is conceptually easy and allows you to extend the items much more. For example, there's nothing stopping you to give each item it's own Font or something.

    As a 'bonus' there is also a TextAlign property which allows you to align the text of all items to whichever side you want (MiddleLeft in the image).



    How it works:

    The ColorListBox class inherits ListBox, and overloads the Items property. Instead of returning MyBase.Items, it returns its own ColorListBoxItemCollection.
    The original base items are then returned by a (private) property baseItems.
    vb.net Code:
    1. Private _Items As ColorListBoxItemCollection
    2.     <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    3.     Public Overloads ReadOnly Property Items() As ColorListBoxItemCollection
    4.         Get
    5.             Return _Items
    6.         End Get
    7.     End Property
    8.  
    9.     'The original items that the user will never see.
    10.     Private ReadOnly Property baseItems() As ObjectCollection
    11.         Get
    12.             Return MyBase.Items
    13.         End Get
    14.     End Property

    The ColorListBoxItemCollection inherits System.Collections.ObjectModel.Collection(Of ColorListBoxItem) and overrides the Set/Remove/InsertItem and ClearItems methods. In those methods, it adds the item to it's collection but also adds the item to the baseItems property of the owner ColorListBox. This is necessary because the ColorListBox won't draw any items in your own custom collection; only those in the MyBase.Items collection.
    vb.net Code:
    1. Protected Overrides Sub ClearItems()
    2.             MyBase.ClearItems()
    3.             _listBox.baseItems.Clear()
    4.         End Sub
    5.  
    6.         Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As ColorListBoxItem)
    7.             MyBase.InsertItem(index, item)
    8.             _listBox.baseItems.Insert(index, item)
    9.         End Sub
    10.  
    11.         Protected Overrides Sub RemoveItem(ByVal index As Integer)
    12.             MyBase.RemoveItem(index)
    13.             _listBox.baseItems.RemoveAt(index)
    14.         End Sub
    15.  
    16.         Protected Overrides Sub SetItem(ByVal index As Integer, ByVal item As ColorListBoxItem)
    17.             MyBase.SetItem(index, item)
    18.             _listBox.baseItems(index) = item
    19.         End Sub

    Finally, the ColorListBox is owner drawn, so it overrides the OnDrawItem method. The listbox actually wants to draw the original items, but I simply force it to use the items from my own collection (by using their index), and draw them with the correct color and possibly with an image.




    As I said, this is the 'base' for a different ListBox control which I'll hopefully also complete shortly.

    I am not 100&#37; sure whether this method will always work (I am afraid the baseItems and Items might become 'de-synchronised' for some reason for example), but I hope it will work fine.


    Enjoy!
    Attached Files Attached Files

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