//消除现有所有内容 ((CComboBox*)GetDlgItem(IDC_COMBOBOX))->ResetContent();
//初始化 for(int i=0; i<=100; i++) { strTemp.Format("%d", i); ((CComboBox*)GetDlgItem(IDC_COMBOBOX))->AddString(strTemp); }
//取得目前已经有的行数 int iCount = ((CComboBox*)GetDlgItem(IDC_COMBOBOX))->GetCount();
其他一些常用的函数:
int iPos=((CComboBox*)GetDlgItem(IDC_COMBO_CF))->GetCurSel();//当前选中的行。
((CComboBox*)GetDlgItem(IDC_COMBO_CF))->SetCurSel(n)//设置第n行内容为显示的内容。
((CComboBox*)GetDlgItem(IDC_COMBO_CF))->GetWindowText(strTemp);//取当前内容
((CComboBox*)GetDlgItem(IDC_COMBO_CF))->GetLBText(n,strTemp);//取其他行内容
DeleteString( UINT nIndex )//删除指定行
InsertString( int nIndex, LPCTSTR lpszItem )//将行插入到指定位置
FindString( int nStartAfter, LPCTSTR lpszItem )//可以在当前所有行中查找指定的字符传的位置,nStartAfter指明从那一行开始进行查找。
int SelectString( int nStartAfter, LPCTSTR lpszItem )//可以选中包含指定字符串的行
为了方便对combobox的操作,通常通过向导类添加一个关联控件的变量m_combo。 也可以手动添加,先在头文件中添加语句 CComboBox m_combo; 然后在DoDataExchange()函数中添加语句 DDX_Control(pDX, IDC_COMBO, m_combo);
CBN_DROPDOWN事件,点击弹出下拉框事件,在这儿可以更新数据,如下:
void CMyControl2::OnDropdownCombo() { // TODO: Add your control notification handler code here //可以在这里更新组合框数据 m_combo.ResetContent(); int i; CString str; static int iflg = 0; if (0==(iflg++%2)) { for (i=0; i<101; i+=2) { str.Format("%d", i); m_combo.AddString(str); } } else { for (i=1; i<101; i+=2) { str.Format("%d", i); m_combo.AddString(str); } } m_combo.SetCurSel(0); }