Winform中ComboBox组件自定义行高

By | 2017年12月14日

默认的ItemHeight很拥挤,看着很别扭,通过自定义控件,在DrawItem事件中定义距离,可以修改行高。

public partial class ComBoxControl : ComboBox
{
    public ComBoxControl()
    {
        InitializeComponent();
        this.DropDownStyle = ComboBoxStyle.DropDownList;
        this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;//重要
    }

    private void ComBoxControl_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index < 0)
        {
            return;
        }
        e.DrawBackground();
        e.DrawFocusRectangle();
        e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
    }
}