问题
I have developed the following custom combo box to increase the height of the items. Once that is done, a blank space appears at the end of the drop down menu when there is a scrollbar.

class MyComboBoxXX : ComboBox
{
public MyComboBoxXX():base()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.MaxDropDownItems = 5;
this.IntegralHeight = false;
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
e.ItemHeight = 40;
this.DropDownHeight = 40 * 5;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
var index = e.Index;
if (index < 0 || index >= Items.Count) return;
using (var brush = new SolidBrush(e.ForeColor))
{
Rectangle rec = new Rectangle(e.Bounds.Left, e.Bounds.Top + ((e.Bounds.Height - ItemHeight) / 2), e.Bounds.Width, ItemHeight);
e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(this.ForeColor), rec);
}
e.DrawFocusRectangle();
}
}
回答1:
If you look closely, it appears that the DropDown area has a 1 pixel border at the top, and also at the bottom. You can get rid of the space by adding 2 pixels to the DropDownHeight
.
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
e.ItemHeight = 40;
this.DropDownHeight = (40 * 5) + 2; //add 2 pixels to include the border
}
Result:

回答2:
I think you should reduce the DropDownHeight value to the appropriate number of items if it's less than the max number in your OnMeasureItem method override.
回答3:
Need to store off the default ItemHeight since changing the DrawMode from Normal causes padding of 2 pixels to be added to the item height. Retrieving the Handle is required prior to the call to ItemHeight since it creates the handle. Without this, the value of the ItemHeight property is not correct. http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ComboBox.cs
来源:https://stackoverflow.com/questions/15080931/ownerdrawvariable-combobox-dropdown-blank-space