您的位置:首页 > 其它

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

2013-07-24 16:03 295 查看
ComboBox 的下拉列表部分总是很挤,看起不舒服,但是设置了 ItemHeight 没用,怎么办呢?

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

然后设置 ComboBox 的 DrawMode 为 OwnerDrawVariable;

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

[csharp]
view plaincopyprint?

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);
}

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);
}


ItemHeight 是设置项的高度,但只设置它没用,为什么呢?因为默认的 DrawMode 决定了它不会有用,所以我们将 DrawMode 设置为 OwnerDrawVariable;然后再自己写 DrawItem 事件处理程序,最后一个参数决定了文字顶端要下移,让文字在选项的中间,看起来舒服些。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: