繁体   English   中英

在winform应用中改变组合框的高度

[英]change height of combobox in winform application

我正在开发一种触摸屏设备的应用程序。 为了方便用户,我需要改变组合框的大小。

我已经检查了很多东西,包括DrawItemEventHandlerMeasureItemEventHandler ,但它没有按我的意愿工作。

基本上我想在不触及字体大小的情况下改变组合框的高度。 当我更改组合框的字体大小时,它看起来像图像的左侧。 如何设置看起来像图像右侧的组合框?

在此输入图像描述

顺便说一句,不知道它是否有效解决方案,我不是使用数组字符串。 我绑定的数据就像。

 combobox.DisplayMember = "Name";
 combobox.ValueMember = "ID";
 combobox.DataSource = new BindingSource { DataSource = datalist };

提前致谢。

有了TaW解决方案,我设法按照自己的意愿设置项目。 当组合框项目没有下降时,我唯一无法在中间设置文本。 如何将此文本位置设置为中心?

在此输入图像描述

您可以设置ItemHeight属性,然后在DrawItem事件中自己绘制项目。

不是很难,搜索'ownerdraw'和'combobox'。 代码项目有一个例子

这是一个最小版本,从上面的链接中提取:

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 ,如果你想为某些itmes设置不同的高度..)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM