I derived a class from ComboBox, made it an owner drawn list box (DrawMode.OwnerDrawVariable style), and by overriding OnMeasureItem and OnDrawItem I add special items to the drop-down list (separators, for example) which are of a smaller/larger size than the normal items that inhabit the control.
Now, the problem I'm having and can't seem to effectively remedy is the sizing of the drop-down list: there's an ugly empty space at the end of the list when it drops down. I thought I tackled this by setting the DropDownHeight property of the ComboBox to the sum of all the items' height, but this doesn't seem to work all the time. Sometimes, on a random number of items, the Empty White Space of Doom returns. This is an unusual problem, but hopefully a common and easily fixed one.
How can I get the ComboBox's drop-down list to size precisely to the size occupied by the items inside of it?
I'm seeing a pretty silly bug in the ComboBox.UpdateDropDownHeight() method. When the DropDownHeight property matches the default value, it calculates a custom height to fit the dropdown to the number of items. It does this even when you changed the DrawMode, that's plain wrong.
The workaround:
int height = ...; // Your code here
if (height == 106) ++height;
comboBox1.DropDownHeight = height;
You'll get a one pixel gap, you should be able to hide that in your OnDrawItem() overload.
I spent a long time battling this same problem.
When you are adding custom items to the combobox, the DropDownHeight will not set properly. In order to guarantee that you get it set right every time, you need to hijack a Windows Message.
This post shows how. Just keep track of the height of all the items (standard and custom) in your combobox, and then set the total height of the dropdown portion like shown in the example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With