Wednesday, March 26, 2008

[VC++, MFC] How to set ComboBox DropDown Height

This is a good example of how messy working with MFC can be. Something apparently so easy could take a lot of time to figure out, so here you can find two easy ways out:

1) Designer - through the designer by default you can just resize the ComboBox width. If you want to resize the Drop Down List height you need to click on the dropdown arrow on the right, then you'll be able to resize the dropped control height. This seems so easy but if no-one tells you it's anything but intuitive. If you just need to statically fix height this'll save you a lot of bitchin'.

2) Programmatically - use the following function to programmatically resize you dropped control height. The function takes as input the number of items you want to display, but if you wanna modify it to take pixels or whatever as parameter it shouldn't be too hard (unless you're brain damaged as Scuffia is).


void SetDropDownHeight(CComboBox* pMyComboBox, int itemsToShow)
{
  //Get rectangles
  CRect rctComboBox, rctDropDown;
  //Combo rect
  pMyComboBox->GetClientRect(&rctComboBox); 
  //DropDownList rect
  pMyComboBox->GetDroppedControlRect(&rctDropDown); 

  //Get Item height
  int itemHeight = pMyComboBox->GetItemHeight(-1); 
  //Converts coordinates
  pMyComboBox->GetParent()->ScreenToClient(&rctDropDown); 
  //Set height
  rctDropDown.bottom = rctDropDown.top + rctComboBox.Height() + itemHeight*itemsToShow; 
  //apply changes
  pMyComboBox->MoveWindow(&rctDropDown); 
}

Keep the butchering up.

38 comments:

Leotardo said...

This saved my fucking life. Thanks

Anonymous said...

Me too! Thanks!

Anonymous said...

Me three! Damn, I have been fighting with that problem for two nights now! My drop down list wouldn't show, I just saw a thin shitty line. At first I thought it was my initialization gone wrong, but then I discovered I could scroll the items into view. It got to the point where I was convinced that it was a Z-ordering issue (because I had other controls just below the list) and that my drop down list was being overlapped/hidden by other controls. This crap just isn't very intuitive at all.

Thanks man!
Kris

Johnny Idol said...

Cheers - I discovered it the hard way myself :)

Anonymous said...

Thanks for the tip! it is great!

JIbin Jerome said...

It really helped me. thanks to whoever had written this code. I have been fighting with that problem for 1 day. Thanks thanks..great work

billy said...

This helped me too! thanks for the info! the butchering continues....

Johnny Idol said...

the butchering never stops :-)

Anonymous said...

Hell yeah!

Two f**** hours on this crappy dropdown -_-.
Still thanks sooooo much for your help!
Keep up the good work ;)

Regards
Idaho

Digital said...

Yes yes, months later, but thanks to your article and Google just saved me a heck of a headache.

Muchas Gracias!

Anonymous said...

You saved my skin!

Bless you

Emil said...

Same here. I would have never guessed it.

Anonymous said...

You are the shit. thanks

Anonymous said...

Two years later and still helpful. Thanks man.

Anonymous said...

"Two years later and still helpful. Thanks man."

I Suscribe to that comment. Thank you very much.

Anonymous said...

MFC :-(

This post :-)

Anonymous said...

linked from google to digg (weird, i know) to here and it's exactly what I needed. click on the dropdown area? wtf.

Anonymous said...

This article is still going strong. Could not for my life figure out this on my own. Many thanks!

Jacob said...

Thanks for this article;

I wonder why is M$ so fucking idiot to make somethng like that and don't even say a shit about how to setup it the height of the combobox.

Again, thanks a lot!

enz said...

This is very useful!!

Anonymous said...

Thank you.

Anonymous said...

Thank you very much

Anonymous said...

4 years later and your code is still saving lives!

You da man!

THANKS!

Adrian said...

I wasted so much time looking for ways around this and getting rid of that stupid scroll bar. Thanks so much!

Guðrún Hauksdóttir said...

Same here. Already spent an hour on this stupid thing!! Thanx a lot :)

Derek said...

Yep, having to deal with old shitty MFC in VS2005, I was tearing my hair out for 30 minutes on this until I found your page. Lifesaver!

Anonymous said...

Wasted an hour before coming across this point. Four years and still going strong...

Anonymous said...

Thanks a ton. I hour down the drain for me :(

Rasmus said...

you are a genius, saved a lot of a lot of time .........................................................................

Anonymous said...

Thank you!

Anonymous said...

Thank you!

mfc lover said...

April 2013, still using these f**** mfc...

Still stuck on the same problems...

Thanks for the tip.

Anonymous said...

2013, you still rock!
Thank you very much for your help

Anonymous said...

Thanks! Thanks! Thanks!!!!

Anonymous said...



Thank you!

Stuart said...

Thanks again!

Anonymous said...

Thx very much !

arsh khan said...

good one