I cannot get the DropDownHeight of the ComboBox set properly to display all the items.
I am using a control that inherits from the ComboBox. I have overridden the OnDrawItem and OnMeasureItem methods in order to create multiple columns and text-wrapping within a column if it is required. This all works fine.
The problem occurs when I try to set the DropDownHeight. I set the DropDownHeight at an arbitrarily large value, a good bit larger than the list of items. The ComboBox control appears to automatically truncate any value for DropDownHeight that is larger than the size of all the displayed items in the list. (Assuming that you have the MaxDropDownItems property set higher than the number of items, which I do.) Normally this behavior works perfectly, as shown below:
alt text http://www.freeimagehosting.net/uploads/dd09404697.png
No, that’s not my real data in the drop-down box.
The problem occurs when I have an entry in the drop-down that needs to wrap in order to display the full text. This entry displays fine, but however the ComboBox is calculating the DropDownHeight, it ignores the fact that one of the entries is twice as tall as normal, so you have to scroll down one line to get to the last entry in the drop-down.
alt text http://www.freeimagehosting.net/uploads/d0ef715f83.png
This is the code that I am using to determine if an item needs text wrapping and to set the height of each item:
Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)
MyBase.OnMeasureItem(e)
//Determine the proper height of the current row in the dropdown based on
//the length of the OptionDescription string.
Dim tmpStr As String = FilterItemOnProperty(Items(e.Index), "OptionDescription")
Dim lng As Single = e.Graphics.MeasureString(tmpStr, Me.Font).Width
//Use the length of the item and the width of the column to calculate if wrapping is needed.
Dim HeightMultiplier As Integer = Math.Floor(lng / _ColumnWidths(1)) + 1
e.ItemHeight = e.ItemHeight * HeightMultiplier
End Sub
I cannot determine how to force the DropDownHeight property to be exactly the value that I want, or how to let the ComboBox control know that one (or more) of the items in the list are taller than normal.
I’ve tried to Override Shadow the DropDownHeight property, but this seemed to have no impact.
EDIT:
Would switching to WPF make this problem go away? (Is there enough customizability in the standard WPF controls so that I don’t need to write a custom control for a 3-column, variable-height combobox?)
I’m trying to solve this exact same problem myself right at the moment for an application that I am migrating from VB6 to VB.NET. The owner-drawn combo control I have in VB6 sets the height of the drop-down through a SetWindowPos API call in response to the WM_CTLCOLORLISTBOX message on the combo control, which gives us access to the HWnd for the drop-down list of the combo control. The following code was added to my class that inherits from ComboBox and seems to do the trick, but still needs testing. I’m not sure it’s the most elegant way of doing this either. Obviously you’ll need to change the line that sets the newHeight variable, but this should give you the general idea.