Combo-box Control - ComboExo example

Create a new ComboExo using VS2013

2025-11-04

Written by: xiaobin

ComboExo example - visualc

The original example was written using VC6.0; now we will rewrite it using VS2013. After downloading, click “Extract” to extract the source files.

Move files to the “res” directory:

Recompile with Visual Studio 2015+

D8016 '/ZI' and '/Gy-' command-line options are incompatible

C/C++ -> General -> Debug Information Format

Program Database(/Zi)

MFC Application Wizard

default

Cancel All

Application Type - Dialog based

MFC standard

none

design dialog

Drag and drop from the toolbox:

List height

There are two ways to adjust the list height:

  1. Drag the “down arrow” down arrow
  2. Configure the rc file
    Original data:
COMBOBOX        IDC_COMBO1,58,39,48,30,CBS_DROPDOWN | CBS_SORT | WS_VSCROLL | WS_TABSTOP

new data: Height of the 4 items

COMBOBOX        IDC_COMBO1,58,39,48,48,CBS_DROPDOWN | CBS_SORT | WS_VSCROLL | WS_TABSTOP

event

IDC_COMBO1 -> Properties

CBN_SELCHANGE

Click the “CBN_SELCHANGE” event property and select ” OnCbnSelchangeCombo1” from the drop-down menu. Sent when the user changes the current selection in the list box of a combo box

void CComboExoDlg::OnCbnSelchangeCombo1()
{
    // TODO: Add your control notification handler code here
    CString strTemp;
    int nCurSel;

    nCurSel = ((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel();
    ((CComboBox*)GetDlgItem(IDC_COMBO1))->GetLBText(nCurSel, strTemp);

    GetDlgItem(IDC_EDIT1)->SetWindowText(strTemp);
}

OnInitDialog()

// Add 20 items to the combo box.
CString str;
for (int i = 0; i < 20; i++)
{
    str.Format(_T("item string %d"), i);
    ((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString(str);
}