Thursday, September 20, 2007

Using OwnerDraw to Provide Item Wrapping in a ListBox.

Rahul Soni's blog - Never assume the obvious is true... - Enable wrap and change the color of Listbox items

"Problem description> You have a Listbox where you have a lot of items which are quite large and won't fit inside the width of your listbox. You don't want to enable Horizontal Scroll bar, since you have don't want your users to keep scrolling left and right in order to view the items. Now if you are able to achieve this, you will notice that it looks pretty ugly since you won't be able to figure out the difference between a wrapped item, and another item. So, you decide to color each item in such a way that item #1 is green, #2 is yellow, #3 is cyan, #4 is green again, and so on...

Have a look at the figure below. The first one is the normal listbox. The 2nd listbox below is the customized version. I think the 2nd one looks much better (although I guess, the color selection could have been much better) Smile
image

..."

I thought this was a pretty cool VB.Net example of tweaking the ListBox to provide wrapping via OwnerDraw and  handling the DrawItem and MeasureItem events.

1 comment:

Arnaud said...

Interesting sample. Hopefully this is much easier to do with WPF. :-)