I'm coding an app in MSVS 2008, which has a ComboBox control which I initialize thru the code as below:
static char* OptionString[4] = {"Opt1",
"Opt2",
"Opt3",
"Opt4"};
BOOL CMyAppDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Option.AddString(OptionString[0]);
m_Option.AddString(OptionString[1]);
m_Option.AddString(OptionString[2]);
m_Option.AddString(OptionString[3]);
m_Option.SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}
In the above code, m_Option
is the Control variable for the ComboBox Control.
Now, when I build the app and click the down-arrow, the drop-down box shows the first option ONLY(since I've selected that thru my code). But, if i press down-arrow key on keyboard, it cycles thru the options in the order I've inserted, but never does it show more than 1 option in the box. So, In case an user wants to select option3, he has to cycle through options 1 and 2 !! Though once I select any option using the keyboard, the appropriate event handlers are fired, I'm miffed by this behaviour , as is understandable.
I'm listing the properties of the combo-box control as well - only the properties that are true(rest are set to false):
This has bugged me for weeks now. Can anyone pls enlighten me ?
You can get or set the combo box's selected item by using the SelectedItem property, and get or set the index of the selected item by using the SelectedIndex property.
The selection in the list box of a combo box is about to be changed as a result of the user either clicking in the list box or changing the selection by using the arrow keys. The combo box receives the input focus. Let us look into an example of Radio button by creating a new MFC dialog based application.
Drag and drop a combo box to store the items, a list box to display the selected items, four button controls to add to the list box with selected items, to fill the combo box, to sort the items and to clear the combo box list, respectively. Add a label control that would display the selected item.
Generally, a combo box is appropriate when there is a list of suggested choices, and a list box is appropriate when you want to limit input to what is on the list. A combo box contains a text box field, so choices not on the list can be typed in. The exception is when the DropDownStyle property is set to DropDownList.
In the dialog layout designer, while designing the dialog, click the "down arrow" on the combobox. You can then drag down on the bottom of the combobox's outline to increase its height.
We can programmatically modify dropdown height as:
CRect rctCmbCountry, rctDropDownCountry;
m_Cmb_Country.GetClientRect(&rctCmbCountry);
m_Cmb_Country.GetDroppedControlRect(&rctDropDownCountry);
itemHeight = m_Cmb_UI_Country.GetItemHeight(-1);
m_Cmb_UI_Country.GetParent()->ScreenToClient(&rctDropDownCountry);
rctDropDownCountry.bottom = rctDropDownCountry.top + rctCmbCountry.Height() + itemHeight * iNoOfITemToShowInComboDropDown;
m_Cmb_UI_Country.MoveWindow(&rctDropDownCountry);
reference: http://codetechnic.blogspot.com/2012/04/vc-mfc-how-to-set-combobox-dropdown.html#:~:text=1)%20Designer%20%2D%20through%20the%20designer,you%20it's%20anything%20but%20intuitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With