Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 20: List-Based Controls


// TODO: Add extra initialization here
CComboBox *Majors = new CComboBox;

Majors->Create(WS_CHILD | WS_VISIBLE |
WS_VSCROLL | CBS_DROPDOWNLIST,
CRect(10, 50, 100, 150), this, 0x1448);

return TRUE; // return TRUE unless you set the focus to a control
}

After creating the control, probably the next action to take consists of creating its items.
To add a string to a combo box, you can call the CComboBox::AddString() method that
uses the same syntax as the CListBox::AddString() member function. Here is an
example:

BOOL CExerciseDlg::OnInitDialog()
{
CDialog::OnInitDialog();

SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
CComboBox *Majors = new CComboBox;

Majors->Create(WS_CHILD | WS_VISIBLE |
WS_VSCROLL | CBS_DROPDOWNLIST,
CRect(10, 50, 100, 150), this, 0x1448);

Majors->AddString("Accounting");
Majors->AddString("Art Education");
Majors->AddString("Finance");
Majors->AddString("Biology");

return TRUE; // return TRUE unless you set the focus to a control
}

Most other methods are implemented as they are for the CListBox class.

Practical Learning: Using Combo Box Methods



  1. To create the list of flavors, in the OnInitDialog() event of the dialog class, type the
    following:


CClarksvilleIceScream2Dlg::CClarksvilleIceScream2Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CClarksvilleIceScream2Dlg::IDD, pParent)
, m_ContainerValue(0)
, m_TaxRate(_T("0.00"))
, m_SubTotal(_T("0.00"))
, m_TaxAmount(_T("0.00"))
, m_OrderTotal(_T("0.00"))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

...

Free download pdf