How do I set the height of a ComboBox?

How do I set the height of a ComboBox?

I have a ComboBox on a form, and its default height is 21. How do I change it?

ComboBox auto-sizes to fit the font. Turning that off is not an option. If you want it bigger then give it a bigger font.

Just as another option, if you’d like to increase the height of the ComboBox without increasing the font size or having to worry about drawing everything yourself, you can use a simple Win32 API call to increase the height like this:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CsvReader
{
    public partial class Form2 : Form
    {
        [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);
        }
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            SetComboBoxHeight(comboBox1.Handle, 50);
            comboBox1.Refresh();
        }
    }
}

Result:

How do I set the height of a ComboBox?
How do I set the height of a ComboBox?

Published by

tuanitpro

Tôi là Lê Thanh Tuấn, và tôi chia sẻ những điều mình cho rằng nó là thú vị, hay giúp ích cho bạn!

Leave a Reply