更改 ComboBox 的项目高度 [英] Change Item Height of ComboBox

查看:35
本文介绍了更改 ComboBox 的项目高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置组合框项目的高度?我的 combobox.size=new size(320,40) 并且我已经设置了 combobox.itemheight=18 但它没有用.我希望我的 itemheight 或文本高度为 18,并且组合框的固定大小为 320x40.我也使用了 drawmode 属性,但什么也没发生.

How to set combobox item height? My combobox.size=new size(320,40) and I had set combobox.itemheight=18 but it didn't work. I want my itemheight or text height to be 18, and fixed size for the combobox which is 320x40. I used also drawmode property but nothing is happening.

推荐答案

好吧,为了防止组合框调整到默认高度,您可以声明它正在手动绘制:

Well, in order to prevent combobox resizing to its default height, you can declare it being manually drawing:

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();
}

编辑:让组合框拉伸,但下拉列表

Edit: to have the combobox stretched, but not its dropdown list

   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
   }

这篇关于更改 ComboBox 的项目高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆