我写了个基于对话框的程序,放了个CComboBox控件,用户在使用时,从ComboBox下拉表中选择一项,如何获取选定项的内容(注意是内容不是索引),并且以字符串类型传出,或者把对应的内容写入一个缓冲区。

解决方案 »

  1.   

    //
    int i = m_Com.GetCurSel();
    if(i!= -1)
    {
    CString rString;
    m_Com.GetLBText(i,rString);
    }
      

  2.   

    还有,在对话框加载时,我想让comboBox的Edit框里显示一条提示语句如”please choose",但我用了SetWindowText和SetDlgItemText试过,都不行,怎么回事呢?
      

  3.   

    m_Com.AddString(”please choose");
    m_Com.SetCurSel(0);
      

  4.   

    Gets a string from the list box of a combo box.int GetLBText(
       int nIndex,
       LPTSTR lpszText 
    ) const;
    void GetLBText(
       int nIndex,
       CString& rString 
    ) const;
    Parameters
    nIndex 
    Contains the zero-based index of the list-box string to be copied. 
    lpszText 
    Points to a buffer that is to receive the string. The buffer must have sufficient space for the string and a terminating null character. 
    rString 
    A reference to a CString. 
      

  5.   

    m_Combo.InsertString(0,"please choose");
    m_Combo.SetCurSel(0);
      

  6.   

    好了,现在可以了,真是谢谢各位了!但还是有问题,执行到fopen那句时,总说文件不存在!但我测试了下ch_filename这个字符串,里面确实拷贝了选定的名字。而且这个文件也确实存在,不知道什么原因。这个问题困扰我一天了,开始以为是获取选定项时不正确,但现在还是这样,晕了...
    void CFomatManageDlg::OnSelchangeComboTem() 
    {
    // 将用户选择的模板显示为界面
        FILE *fp;
    //MyComboBox myComboBox;
        CComboBox *pMyComboBox=(CComboBox*)GetDlgItem(IDC_COMBO_TEM);
    int i=pMyComboBox->GetCurSel();
    CString ch_filename;
    pMyComboBox->GetLBText(i,ch_filename);
    if((fp=fopen(ch_filename,"r"))==NULL)
    {
    MessageBox("This file doesn't exist!","error",MB_OK);
    }
    }