BOOL CDialog::OnInitDialog() { // Be careful to call CDialog::OnInitDialog // only once in this function CListBox* pLB = (CListBox*) GetDlgItem(IDC_DEPT); pLB->InsertString(-1, "Documentation"); pLB->InsertString(-1, "Accounting"); pLB->InsertString(-1, "Human Relations"); pLB->InsertString(-1, "Security");
// The pointer to my list box. extern CListBox* pmyListBox;// Add 10 items to the list box. CString str; for (int i=0;i < 10;i++) { str.Format(_T("item string %d"), i); pmyListBox->AddString( str ); }
{
// Be careful to call CDialog::OnInitDialog
// only once in this function
CListBox* pLB = (CListBox*) GetDlgItem(IDC_DEPT);
pLB->InsertString(-1, "Documentation");
pLB->InsertString(-1, "Accounting");
pLB->InsertString(-1, "Human Relations");
pLB->InsertString(-1, "Security");
return CDialog::OnInitDialog();
}
重载OnInitDialog()函数,IDC_DEPT是listbox对话框的id
extern CListBox* pmyListBox;// Add 10 items to the list box.
CString str;
for (int i=0;i < 10;i++)
{
str.Format(_T("item string %d"), i);
pmyListBox->AddString( str );
}
也可以用InsertString(索引,你要的字符串)