Knowledge Base Nr: 00310 fillcombo.cpp - http://www.swe-kaiser.de

MFC: Combobox füllen und default anwählen

  
//sample usage:
CString sSprache = "deutsch";
CString sSprachen = "englisch#deutsch#französisch#";
FillComboBox((CComboBox*)GetDlgItem(IDC_LANGUAGE), sSprache, sSprachen);

//implementierung:
void CHelperFunc::FillComboBox(CComboBox* pWndCB, const CString sSelValue, const CString sEnumValues)
{
ASSERT(IsWindow(pWndCB->GetSafeHwnd()));

pWndCB->Clear();
pWndCB->ResetContent();

CString sEnums = sEnumValues;
int nPos = 0;
while ((nPos = sEnums.Find('#')) >=0)
{
CString sEnum = sEnums.Left(nPos);
int n = pWndCB->AddString(sEnum);
if (sEnum == sSelValue)
pWndCB->SetCurSel(n);
sEnums = sEnums.Mid(nPos+1);
}
}