Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设置组合框的高度?_C#_.net_Winforms_Combobox - Fatal编程技术网

C# 如何设置组合框的高度?

C# 如何设置组合框的高度?,c#,.net,winforms,combobox,C#,.net,Winforms,Combobox,我在表单上有一个组合框,它的默认高度是21。如何更改它?在代码中,a.高度应该起作用。在设计器中,进入属性并查看大小->高度 或者,您可以更改字体大小,组合框将变大以容纳它,但我认为这不是您想要的。将绘图模式设置为OwnerDrawVariable。但是,组合框的定制会导致其他问题。有关如何完成此操作的教程,请参见此链接: OwnerDrawVariable此处的示例代码: 完成后,需要设置combobox的ItemHeight属性以设置combobox的有效高度 组合框自动调整大小以适应字

我在表单上有一个组合框,它的默认高度是21。如何更改它?

在代码中,a.高度应该起作用。在设计器中,进入属性并查看大小->高度


或者,您可以更改字体大小,组合框将变大以容纳它,但我认为这不是您想要的。

绘图模式设置为
OwnerDrawVariable
。但是,组合框的定制会导致其他问题。有关如何完成此操作的教程,请参见此链接:

OwnerDrawVariable
此处的示例代码:


完成后,需要设置combobox的
ItemHeight
属性以设置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();
        }
    }
}
结果:


如果您想调整组合框中的项目数,可以根据项目列表更改下拉高度值,如下所示。我在这里使用24作为“每项金额”;这绝不是固定的

  comboBox1.DropDownHeight = SomeList.Count * 24;

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

此示例将允许您使用组合框的
ItemHeight
属性,而不考虑字体大小。我添加了一个奖金属性
TextAlign
,它还允许您将项目居中

值得一提的是,您必须将所选项目的
DropDownStyle
设置为
DropDownList
,以尊重我们的定制

// 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.
    }

}
现在我们可以分别完全控制组合框的字体和高度。我们不再需要使用大字体来调整组合框的大小


组合框有一个属性“DropDownHeight”,可以通过组合框的“属性”窗口更改,也可以通过编程方式更改。i、 e

public partial class EventTestForm : Form
{
    public EventTestForm()
    {
        InitializeComponent();
        cmbOwners.DropDownHeight = 100;
    }

你的链接不再有效,你能总结一下内容吗?@AnyaHope:我更新了MSDN链接,其中包含一些示例代码。使用示例代码设置一个项目,如我所述,通过添加
ItemHeight
来补充示例,您应该很好。没有效果。运行64位Windows 10。它只是让组合框看起来很不现代,就好像是在所有者绘制模式下。对我来说很有用。赢10 64位,任何CPU构建。我确实需要小心计时…我无法在自定义ComboBox控件的构造函数中运行它,我在第一次调用“DrawItem”时就使用了它。另外,
DrawMode=DrawMode.OwnerDrawVariable
也在Win 10 64位上为我工作。感谢要根据项目数调整高度,请参见属性DropDownHeight。