mshvw

Hi,

My Form is resizable and a ComboBox on it must resize accordingly.

Currently I adjust the ComboBox.Font size after each Form resizing.

There is always some space above and below the text in the ComboBox text area.

I would like to make the ComboBox vertically fit neatly to the text inside the ComboBox so the ComboBox.Height will be a little smaller.

How can I achieve this

H.



Re: Visual C# IDE How make ComboBox text fit neatly?

Citizen on the earth

Hi mshvw,

As I understand, you would like to resize the height of the ComboBox when a form is resizable.

You can try to refer to the following sample codes to resize the height of a ComboBox:

Code Block

public void SetComboEditHeight(ComboBox cmb,Int32 newHeight)

{

SendMessage(cmb.Handle, CB_SETITEMHEIGHT, -1, newHeight);
cmb.Refresh();

}

[DllImport("user32.dll")]
public static extern int SendMessage(
int hWnd, // handle to destination window
uint Msg, // message
long wParam, // first message parameter
long lParam // second message parameter

);

Hope this helps,

Regards,

Citizens on the earth





Re: Visual C# IDE How make ComboBox text fit neatly?

Tharmapalan

Hi mshvw

u can solve it by easy way that is changes propery of anchor to the combo box

set up combo box's anchor property as Top, Left, Right it automatically resize ur combo box depend on the form size

Regards

palan





Re: Visual C# IDE How make ComboBox text fit neatly?

mshvw

How do I get this CB_SETITEMHEIGHT constant get recognized by my compiler





Re: Visual C# IDE How make ComboBox text fit neatly?

Citizen on the earth

Hi mshvw,

You can declare "CB_SETITEMHEIGHT" constant as follows:

Const CB_SETITEMHEIGHT = &H153

Hope this helps,

Regards,

Citizens on the earth





Re: Visual C# IDE How make ComboBox text fit neatly?

mshvw

Well, I know, but that's not a real resize.

When anchoring, then if I double the Form.Height then my Control.Height will not double. It's Height will increase with half of the new Form.Height.
On the other hand, a ComboBox cannot be resized vertical. The ComboBox.Height is automatically set according it's Font.Size.

H.





Re: Visual C# IDE How make ComboBox text fit neatly?

Citizen on the earth

Hi mshvw,

Thanks for your quick reply.

As far as I know, there is another option for you to change the height of a ComboBox, that is to say that you make it by owner-draw.

Hope this helps,

Regards,

Citizens on the earth





Re: Visual C# IDE How make ComboBox text fit neatly?

mshvw

Hi,

I finally got it working, you probably assumed I use VB but I use C#.
I had to rearange some to get it work without compile errors.

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(
int hWnd, // handle to destination window
uint Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);

public void SetComboEditHeight(ComboBox cmb,Int32 newHeight){
SendMessage((int)cmb.Handle, myConstants.CB_SETITEMHEIGHT, (IntPtr)(-1), (IntPtr)newHeight);
cmb.Refresh();
}

The 'int' argument 3 and 4 had to be replaced by 'IntPtr' to avoid runtime error.

Thanks,

H.





Re: Visual C# IDE How make ComboBox text fit neatly?

mshvw

Hi,

as a follow-up question,

can I also move-up the text in the combobox so it gets closer to the upper border of the combobox

H.




Re: Visual C# IDE How make ComboBox text fit neatly?

Citizen on the earth

Hi mshvw,

As I understand, it is not easy to move the text in the ComboBox and make it being close to the upper border of the ComboBox. If so, you can try to draw the items of the ComboBox by owner-draw.

Hope this helps,

Regards,

Citizens on the earth