更改ComboBox的项目高度

时间:2016-02-11 06:44:13

标签: c# winforms

如何设置组合框项目高度?我的combobox.size =新尺寸(320,40),我设置了combobox.itemheight = 18,但它不起作用。我希望我的itemheight或文本高度为18,并且组合框的固定大小为320x40。我也使用了drawmode属性,但没有发生任何事情。

2 个答案:

答案 0 :(得分:1)

好吧,为了防止组合框大小调整到默认高度,可以声明它是手动绘制的:

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

修改:让组合框拉伸,但其下拉列表

   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
   }

答案 1 :(得分:0)

尝试更改组合框的字体大小