更改组合框的项目高度
本文关键字:项目 高度 组合 | 更新日期: 2023-09-27 18:34:18
如何设置组合框项目高度?我的 combobox.size=new size(320,40) 和我设置了 combobox.itemheight=18,但它不起作用。我希望我的项目高度或文本高度为 18,组合框的固定大小为 320x40。我还使用了绘图模式属性,但什么也没发生。
尝试更改组合框的字体大小
好吧,为了防止组合框调整其默认高度,您可以声明它正在手动绘制:
myComboBox.DrawMode = DrawMode.OwnerDrawFixed; // or DrawMode.OwnerDrawVariable;
myComboBox.Height = 18; // <- what ever you want
然后你必须实现DrawItem
事件:
private void myComboBox_DrawItem(object sender, DrawItemEventArgs e) {
ComboBox box = sender as ComboBox;
if (Object.ReferenceEquals(null, box))
return;
e.DrawBackground();
if (e.Index >= 0) {
Graphics g = e.Graphics;
using (Brush brush = ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
? new SolidBrush(SystemColors.Highlight)
: new SolidBrush(e.BackColor)) {
using (Brush textBrush = new SolidBrush(e.ForeColor)) {
g.FillRectangle(brush, e.Bounds);
g.DrawString(box.Items[e.Index].ToString(),
e.Font,
textBrush,
e.Bounds,
StringFormat.GenericDefault);
}
}
}
e.DrawFocusRectangle();
}
编辑:拉伸组合框,但不拉伸其下拉列表
myComboBox.DrawMode = DrawMode.OwnerDrawVariable;
myComboBox.Height = 18; // Combobox itself is 18 pixels in height
...
private void myComboBox_MeasureItem(object sender, MeasureItemEventArgs e) {
e.ItemHeight = 17; // while item is 17 pixels high only
}