如何设置 ComboBox 下拉列表的高度或间距

2025-04-24 08:26:42
推荐回答(1个)
回答1:

首先设置一个较大的 ItemHeight 值,比如 20;

然后设置 ComboBox 的 DrawMode 为 OwnerDrawVariable;

然后在 DrawItem 事件中实现如何代码:

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

temHeight 是设置项的高度,但只设置它没用,为什么呢?因为默认的 DrawMode 决定了它不会有用,所以我们将 DrawMode 设置为 OwnerDrawVariable;然后再自己写 DrawItem 事件处理程序,最后一个参数决定了文字顶端要下移,让文字在选项的中间,看起来舒服些