Fog Creek Software
Discussion Board




Why is there an extra pseudo-item in a listbox?

Create a Windows listbox

Make it exactly big enough (or even a few  pixels bigger just to be safe) for N items.

Fill the list with N items

What happens?

The scrollbar appears, sized, as if the list contains N+1 items.

But when you scroll - there is of course no extra just a blank gap for a potential item after the last actual item.

It's almost as if the listbox scrollbar things there's an extra imaginary-item after the last actual item.

- Is this is a bug or a feature in Windows?

- Is there any good reason why it was designed this way?

- Is there any way to make the listbox scrollbar not think there's an extra imaginary-item after the last actual item.




(Note: I've only been tested this thoroughly in certain combinations of Listbox styles, and Windows versions, etc, so the it is possible this bug/feature doesn't always appear or is fixed in later Windows versions)

S. Tanna
Saturday, July 24, 2004

I had to stare at your question for a few moments to grasp the issue.

Sometimes it appears that there is a phantom first item in an unpopulated list box, but this is probably just so that the listbox has something to display in its interior and is not really a list item.

You seem to really be asking about the scroll bar behavior. Scroll bars are a separate Windows control. In fact there is a scroll bar available in Windows that can be placed by itself. The scrollbar control is parameterized with a minimum and maximum data value (range) and a current position ,as well as values for line and page stepping (the amount by which the scroll moves if you click on an end button or on the interior of the scroller outside the slider button.)

My guess is that unless the client area of the listbox is an exact integer multiple of the list item height, then the scrollbar will always be programmed to have a range of lines one greater than the visible count of lines. Why: so that the final item is always accessible by the scroll bar.

Bored Bystander
Saturday, July 24, 2004

>> scrollbar will always be programmed to have a range of lines one greater

That is, the scroller is PROGRAMMED to GENERATE data values that are one greater than... yatta yatta...

Bored Bystander
Saturday, July 24, 2004

Scrollbars are sometimes not implemented as distinct child controls:

http://weblogs.asp.net/oldnewthing/archive/2004/05/10/129068.aspx

Alex
Saturday, July 24, 2004

I had another issue with listboxes. They seem to try to adjust
their size so that even number of items is completely visible.

I tried to create a listbox and a simple pushbutton next to
it and they both had to align at the bottom. But it didn't
work. List was always below or above the button until I
started calculating these positions myself and adjusted
both controls to align nicely.

There must be a rule about it I suppose but I couldn't find it
documented.

VPC
Saturday, July 24, 2004

In this particular case, the scrollbar is not a separate control but triggered by a style associated with the listbox.

The range of the scrollbar is, in this case, set by Windows itself, in whatever code in Windows handles the listbox.  And it is this that I am questioning, as I can't find rhyme nor reason to the range it chooses.

However, after some experimenting I have hit on a combination of other style settings and sizing, that seems to eliminate the problem. (Whcih solves my immediate issue).

That said, it's still an open question for me, as pure intellectual one.

S. Tanna
Saturday, July 24, 2004

LBS_NOINTEGRALHEIGHT

Raymond Chen
Sunday, July 25, 2004

Thanks,

LBS_NOINTEGRALHEIGHT magically appeared in MSDN, Win32.hlp by Borland and in several books I have.

I can swear it wasn't there before;)

VPC
Sunday, July 25, 2004

*  Recent Topics

*  Fog Creek Home