我想将 ComboBox
(来自 Windows 窗体 Control s)的高度设置为 X在运行时。
我有
- 将
DrawMode
属性设置为OwnerDrawVariable
,以指定 项目是手动绘制的; - 将
IntegralHeight
属性设置为 false,以避免Control
自动调整自身大小; ComboBox
的ItemHeight
属性也设置为 X。
我还覆盖了组合框项目的 DrawItem
和 MeasureItem
事件(参见下面的代码)
但是,只有当 DropDownStyle
设置为 Simple
时,在运行时设置 ComboBox 的 Height
才有效。
我以两种方式执行此操作,以编程方式(修改 ComboBox
的 Height
或 Size
)属性,并使用 PropertyGrid
控制我在我的应用程序上拥有的内容。
当我设置 DropDown
或 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
}
最佳答案
namespace WinForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.Size = new Size(10,10);
}
}
}
您还可以在 Visual Studio 的属性 View 中更改 Size(Width,Height)
关于C# - 设置 ComboBox 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11123614/