我想在 runtime 期间ComboBox
将 a (从 Windows Forms Control s)的高度设置为X 。
我有
- 将
DrawMode
Property 设置为OwnerDrawVariable
,以指定手动绘制项目; - 将
IntegralHeight
Property 设置为 false,以避免Control
自动调整大小; - 的
ItemHeight
PropertyComboBox
也设置为X。
我还覆盖了组合框项目的DrawItem
和MeasureItem
事件(参见下面的代码)
但是,在运行时设置ComboBox 的 仅在设置为时才有效。Height
DropDownStyle
Simple
我这样做是两种方式,以编程方式(修改ComboBox
'sHeight
或Size
)属性,并使用PropertyGrid
我在我的应用程序上拥有的控件。
DropDown
当我设置 a or的高度时DropDownList
ComboBox
,我注意到 Windows 会自动将其高度更改为另一个值:Y(经过一些调试后,由于某种原因,我注意到它是X + 6)。
我错过了什么?为什么会这样?
以下是处理程序的代码:
private void DrawItemHandler(object sender, DrawItemEventArgs e)
{
//Create a new font to write the item
Font ComboItemFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular);
e.DrawBackground();
e.DrawFocusRectangle();
//Write the item's value
e.Graphics.DrawString(((ComboBox) sender).Items[e.Index].ToString(),
ComboItemFont,
new SolidBrush(Color.Black),
e.Bounds);
//Update the source's font to match the current font
((Control) sender).Font = ComboItemFont;
}
private void MeasureItemHandler(object sender, MeasureItemEventArgs e)
{
//Do nothing
}