开发者

MFC Combo-Box Control is not showing the full list of items when I click the drop-down menu

开发者 https://www.devze.com 2022-12-25 02:00 出处:网络
I\'m coding an app in MSVS 2008, which has a ComboBox control which I initialize thru the code as below:

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 TR开发者_高级运维UE  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):

  1. Type - Dropdown
  2. Vertical Scrollbar
  3. Visible Tabstop

This has bugged me for weeks now. Can anyone pls enlighten me ?


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.


You need to increase the height of the drop down of combo box in 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.

Hope you understood my point.


Another method to set height of the drop down of combo box is to manualy edit rc file. You can set 5th parameter which is responsible for height of the drop down (72 in this example).

COMBOBOX   IDC_COMBOBOX1,17,35,157,72,CBS_DROPDOWN | CBS_SORT | WS_VSCROLL | WS_TABSTOP


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.


I also was suffered by this problem and finally I found the solution for my MFC applications. The problem is that I did not apply the manifest version 6 to my applications. To solve this problem, I added the code to mark the manifest as following:

ifdef _UNICODE
if defined _M_IX86
pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
elif defined _M_X64
pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
else
pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
endif
endif

It worked well. You can refer to the link [here] (http://msdn.microsoft.com/en-us/library/windows/desktop/bb773175%28v=vs.85%29.aspx) for more details. Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消