Home Articles Books Downloads FAQs Tips

Q: Force a ComboBox to drop down


Answer:

To tell you the truth, I decided that this would be a good FAQ addition because I see this question pop up frequently in MFC FAQ's, newsgroups, and discussion boards (MFC groups are a good place to learn about the API, since MFC doesn't encapsulate a whole heck of a lot). However, while researching how to force a ComboBox to drop down in C++Builder, I realized that this is so simple that it probably shouldn't be an FAQ. Oh well.

To drop a ComboBox from code, simply set the DroppedDown property of the ComboBox to true. Here is some code:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    ComboBox1->DroppedDown = true;
}

Note: The DroppedDown property also works in reverse. If you need to check if a ComboBox is dropped down, you can read the DroppedDown property. It will be true if the ComboBox is dropped down.

Note: The write method of the DroppedDown property works by sending a message to the window handle of the ComboBox. Just in case your curious, here is what the VCL code looks like (pascal converted to C++):

void _fastcall TCustomComboBox::SetDroppedDown(bool Value)
{
    // NOTE: must cast bool to pass Value as the WPARAM
    //       of the message. The VCL actually casts to LongInt.
    SendMessage(Handle, CB_SHOWDROPDOWN, (WPARAM)Value, 0);
}


Copyright © 1997-2000 by Harold Howe.
All rights reserved.