我正在为一种触摸屏设备开发应用程序。为了用户友好,我需要更改组合框的大小。
我已经检查了很多东西,包括 DrawItemEventHandler
和 MeasureItemEventHandler
,但它并没有像我想要的那样工作。
基本上我想在不改变字体大小的情况下改变组合框的高度。当我更改组合框的字体大小时,它看起来像图像的左侧。 如何设置组合框使其看起来像图像的右侧?
对了,不知道是不是有效的解决方法,我用的不是array string。我正在绑定(bind)数据。
combobox.DisplayMember = "Name";
combobox.ValueMember = "ID";
combobox.DataSource = new BindingSource { DataSource = datalist };
提前致谢。
使用 TaW 解决方案,我可以根据需要设置项目。当组合框项目未下拉时,我唯一无法在中间设置文本。如何将此文本位置设置为中心?
最佳答案
您可以设置ItemHeight
属性,然后在DrawItem
事件中自行绘制项目。
不是很难,搜索“ownerdraw”和“combobox”。 Code Project 上有一个例子
这是从上面的链接中提取的最小版本:
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) return;
Font f = comboBox1.Font;
int yOffset = 10;
if ((e.State & DrawItemState.Focus) == 0)
{
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), f, Brushes.Black,
new Point(e.Bounds.X, e.Bounds.Y + yOffset));
}
else
{
e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), f, Brushes.White,
new Point(e.Bounds.X, e.Bounds.Y + yOffset));
}
您还必须将 DropDownStyle
设置为 DropDownList
才能使突出显示工作,并且您需要将 DrawMode
设置为 OwnerDrawFixed
。 (或者到 OwnerDrawVariable
,如果你想为某些项目设置不同的高度......)
关于c# - 在 Winform 应用程序中更改组合框的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22712944/