serhads
serhads

Reputation: 462

change height of combobox in winform application

I'm developing an application for kind of touch screen device. In order be user friendly, I need to change size of combobox.

I've checked many thing including DrawItemEventHandler and MeasureItemEventHandler, but it didn't work as I want.

Basically I would like to change height of combobox without touching font size. When I change font size of combobox, it looks like left side of the image. How can I set my combobox which will look like right side of the image?

enter image description here

By the way, don't know if it's effect solution, I am not using array string. I'm binding data like.

 combobox.DisplayMember = "Name";
 combobox.ValueMember = "ID";
 combobox.DataSource = new BindingSource { DataSource = datalist };

Thanks in advance.

With TaW solution, I managed to set items as I want. The only thing I couldn't set text in middle when combobox items not droped down. How can I set this text position to the centre?

enter image description here

Upvotes: 3

Views: 9252

Answers (1)

TaW
TaW

Reputation: 54433

You can set the ItemHeight property and then draw the items yourself in the DrawItem event.

Not terribly hard, search for 'ownerdraw' & 'combobox'. There is one example on Code Project

Here is a minimal version, pulled from the above link:

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index < 0) return;
    Font f = comboBox1.Font;
    int yOffset = 10;

    if ((e.State & DrawItemState.Focus) == 0)
    {
        e.Graphics.FillRectangle(Brushes.White, e.Bounds);
        e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), f, Brushes.Black, 
                              new Point(e.Bounds.X, e.Bounds.Y + yOffset));
    }
    else
    {
        e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);
        e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), f, Brushes.White, 
                              new Point(e.Bounds.X, e.Bounds.Y + yOffset));
    }

}

You also have to set the DropDownStyle to DropDownList to get the highlighting to work and you need to set the DrawMode to OwnerDrawFixed. (Or to OwnerDrawVariable, if you want to have different heights for some itmes..)

Upvotes: 6

Related Questions