mfc控件使用--- combobox 控件

//消除現有全部內容
((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();

 

 

其餘一些經常使用的函數:web

  int iPos=((CComboBox*)GetDlgItem(IDC_COMBO_CF))->GetCurSel();//當前選中的行。函數

  ((CComboBox*)GetDlgItem(IDC_COMBO_CF))->SetCurSel(n)//設置第n行內容爲顯示的內容。spa

  ((CComboBox*)GetDlgItem(IDC_COMBO_CF))->GetWindowText(strTemp);//取當前內容code

  ((CComboBox*)GetDlgItem(IDC_COMBO_CF))->GetLBText(n,strTemp);//取其餘行內容orm

  DeleteString( UINT nIndex )//刪除指定行blog

  InsertString( int nIndex, LPCTSTR lpszItem )//將行插入到指定位置事件

  FindString( int nStartAfter, LPCTSTR lpszItem )//能夠在當前全部行中查找指定的字符傳的位置,nStartAfter指明從那一行開始進行查找。 
  int SelectString( int nStartAfter, LPCTSTR lpszItem )//能夠選中包含指定字符串的行ci

 

  爲了方便對combobox的操做,一般經過嚮導類添加一個關聯控件的變量m_combo。 也能夠手動添加,先在頭文件中添加語句 CComboBox m_combo; 而後在DoDataExchange()函數中添加語句 DDX_Control(pDX, IDC_COMBO, m_combo);pdo

  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);
}
相關文章
相關標籤/搜索