如何设置ComboBox的高度?

我在表单上有一个ComboBox,其默认高度为21.如何更改它?

ComboBox自动resize以适应字体。 关闭它不是一种选择。 如果你想要它更大,那么给它一个更大的字体。

DrawMode设置为OwnerDrawVariable 。 但是,ComboBox的自定义会导致其他问题。 有关如何完成此操作的教程,请参阅此链接:

http://www.csharphelp.com/2006/09/listbox-control-in-c/

OwnerDrawVariable示例代码: https : //msdn.microsoft.com/en-us/library/system.windows.forms.combobox.drawitem%28v=vs.110%29.aspx

完成后,您需要设置combobox的ItemHeight属性以设置combobox的有效高度。

正如另一种选择,如果您想在不增加字体大小的情况下增加ComboBox的高度或者不必担心自己绘制所有内容,可以使用简单的Win32 API调用来增加高度,如下所示:

 using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace Win32ComboBoxHeightExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam); private const Int32 CB_SETITEMHEIGHT = 0x153; private void SetComboBoxHeight(IntPtr comboBoxHandle, Int32 comboBoxDesiredHeight) { SendMessage(comboBoxHandle, CB_SETITEMHEIGHT, -1, comboBoxDesiredHeight); } private void button1_Click(object sender, EventArgs e) { SetComboBoxHeight(comboBox1.Handle, 150); comboBox1.Refresh(); } } } 

结果:

在此处输入图像描述

如果您想要调整ComboBox中的项目数,您可以在给定项目列表的情况下更改DropDownHeight的值,如下所示。 我在这里使用24作为“每件物品”; 这绝不是固定的。

  comboBox1.DropDownHeight = SomeList.Count * 24; 

为此,您需要将DrawMode设置为OwnerDrawVariableOwnerDrawFixed并手动绘制项目。 这可以通过一个非常简单的类来完成。

此示例将允许您使用ComboBox的ItemHeight属性,而不管字体大小。 我投入了一个奖金属性TextAlign ,它也可以让你将物品居中。

值得一提的是,您必须为所选项目设置DropDownStyleDropDownList以遵守我们的自定义。

 // The standard combo box height is determined by the font. This means, if you want a large text box, you must use a large font. // In our class, ItemHeight will now determine the height of the combobox with no respect to the combobox font. // TextAlign can be used to align the text in the ComboBox class UKComboBox : ComboBox { private StringAlignment _textAlign = StringAlignment.Center; [Description("String Alignment")] [Category("CustomFonts")] [DefaultValue(typeof(StringAlignment))] public StringAlignment TextAlign { get { return _textAlign; } set { _textAlign = value; } } private int _textYOffset = 0; [Description("When using a non-centered TextAlign, you may want to use TextYOffset to manually center the Item text.")] [Category("CustomFonts")] [DefaultValue(typeof(int))] public int TextYOffset { get { return _textYOffset; } set { _textYOffset = value; } } public UKComboBox() { // Set OwnerDrawVariable to indicate we will manually draw all elements. this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; // DropDownList style required for selected item to respect our DrawItem customizations. this.DropDownStyle = ComboBoxStyle.DropDownList; // Hook into our DrawItem & MeasureItem events this.DrawItem += new DrawItemEventHandler(ComboBox_DrawItem); this.MeasureItem += new MeasureItemEventHandler(ComboBox_MeasureItem); } // Allow Combo Box to center aligned and manually draw our items private void ComboBox_DrawItem(object sender, DrawItemEventArgs e) { // Draw the background e.DrawBackground(); // Draw the items if (e.Index >= 0) { // Set the string format to our desired format (Center, Near, Far) StringFormat sf = new StringFormat(); sf.LineAlignment = _textAlign; sf.Alignment = _textAlign; // Set the brush the same as our ForeColour Brush brush = new SolidBrush(this.ForeColor); // If this item is selected, draw the highlight if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) brush = SystemBrushes.HighlightText; // Draw our string including our offset. e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, brush, new RectangleF(e.Bounds.X, e.Bounds.Y + _textYOffset, e.Bounds.Width, e.Bounds.Height), sf); } } // If you set the Draw property to DrawMode.OwnerDrawVariable, // you must handle the MeasureItem event. This event handler // will set the height and width of each item before it is drawn. private void ComboBox_MeasureItem(object sender,System.Windows.Forms.MeasureItemEventArgs e) { // Custom heights per item index can be done here. } } 

现在我们可以完全控制ComboBox的字体和高度。 我们不再需要制作一个大字体来调整我们的ComboBox。

在此处输入图像描述

在代码中,a.Height应该工作。 在设计师中,进入属性并查看Size-> Height。

或者,您可以更改字体大小,combobox将变得更大以适应它,但我不认为这是你想要的。