views:

214

answers:

0

I have a baffling problem that I've been stuck on for hours and can't find a fix for. I have inherited a class from the ListBox class so that I can create multi-line and multi-coloured item content. I have the following in the MeasureItem method:

SizeF headingSize = e.Graphics.MeasureString(exam.Name, HeadingFont, ClientSize.Width);
SizeF descriptionSize = e.Graphics.MeasureString(exam.Description, Font, ClientSize.Width);
e.ItemHeight = (int)(headingSize.Height + descriptionSize.Height);

I have also tried DisplayRectangle.Width instead of ClientSize.Width, but they both give the same value.

Anyway, the problem is this: When the very first item is measured, ClientSize.Width = the internal width - the scrollbar width. When the next few items are measured, ClientSize.Width = the internal width of the ListBox WITHOUT a scrollbar. Then from the first item that doesn't fit into the viewable area of the ListBox onwards, the ClientSize.Width = the internal width - the scrollbar width. This results in the first few items getting the wrong ItemHeight values, as although there was no scrollbar counted when they were measured, there is when they are displayed.

The code otherwise works fine... if all items fit into the ListBox, then they are all measured and displayed correctly.

So, does anybody know a way around this? I would even accept a hack like forcing the vertical scrollbar to stay visible always, but I couldn't find a way to do that either. :(

Any help would be greatly appreciated. Many thanks in advance.