从数据库中取出如下多条记录,目标:在combobox中显示A1,A2...,选择A1后,取得B1值,A1,B1都为字符串.A1 B1
A2 B2
A3 B3
...==================================
int i=0;
_variant_t var;
CString strA,strB;
CComboBox  m_Box;while(!m_pRecordset->ADOEOF)
{
  var = m_pRecordset->GetCollect("A");
  if(var.vt != VT_NULL)
     strA = (LPCSTR)_bstr_t(var);
  
  var = m_pRecordset->GetCollect("B");
  if(var.vt != VT_NULL)
     strB = (LPCSTR)_bstr_t(var);  /////////////////
  //就是这这里(还有取值的时候)不知道该怎么写?
  i = m_CBox.AddString(strA);
    ////////////////

  m_pRecordset->MoveNext();
}
==================================

解决方案 »

  1.   

    i=m_CBox.getSelect
    在检索数据库,得到B1
      

  2.   

    int nItem = m_CBox.GetCurSel();
    CString strText;
    m_CBox.GetLBText(nItem, strText);
      

  3.   

    我是想选取A1,得到B2的值.
    那么我在给CComboBox m_Box赋值时代码该怎么写呢?
      

  4.   

    gothing() :
    是的,我希望能只用一个CComboBox.wuxfBrave:
    我比较菜,有时间的话能给出具体点的代码嘛?谢谢.
      

  5.   

    将值A1(如果不是字串,就转换成字串)AddString进组合框里去。
    然后将用SetItemData函数将B1加入到A1项目的附加存储空间中。
    这样A1和B1就通过列表的索引值对应起来。
      

  6.   

    casinosun(casinosun) 说的已经可以实现;楼主这样做,是避免了对数据库的再次查询操作,我的想法是:
    楼主的代码不变,在取出一条记录A和对应的B后,A显示在ComboBox中,同时B保存在CStringList类表中。在得到A1后,CStringList中查找相应的B1。
      

  7.   

    放两个combox 上去,一个可见,一个不可见,
    在可见的那个里选 a1则得到索引值,对应不可见的那个combox,就的到b1
      

  8.   

    arvid_gs(west) 
    我同意他的说法,好像的用SetItemData和GetItemData
      

  9.   

    谢谢各位帮忙 :)我想使用softworms说的办法,在我上面的代码中加入如下代码:i = m_Capture.AddString(strA);
    m_Capture.SetItemData(i,strB);但是编译出错:cannot convert parameter 2 from 'class CString' to 'unsigned long'
    请问该怎么解决啊?
      

  10.   

    i = m_Capture.AddString(strA);
    m_Capture.SetItemData(i,(LPCTSTR)strB);
      

  11.   

    还是编译出错:(
    E:\VC\ADOComboBox\ADOComboBoxDlg.cpp(249) : error C2664: 'SetItemData' : cannot convert parameter 2 from 'const char *' to 'unsigned long'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast
      

  12.   

    SetItemData的第二个参数类型错误,需要DWORD类型,怎么得到的我也向大侠学习。
      

  13.   

    最后用gothing()兄的实现了,万分感谢现定义一个成员变量:
    CStringList m_strBs;在前面的代码中加入:
    i = m_Box.AddString(strA);
    m_strBs.AddTail(strB);选择不同的A值时:
    void CADOComboBoxDlg::OnSelchange() 
    {
    int i;
    i = m_Box.GetCurSel();

    POSITION pos;
    if((pos = m_strBs.FindIndex(i)) != NULL)
    { m_strB = m_strBs.GetAt(pos);}

    UpdateData(FALSE);
    }