要求:
一张天气实况表的对话框里有一个Listcotrol控件:
对话框的对应的类为    CShiKuang
Listcotrol 的实例为m_list
我是在另外一个类里面对listcontrol 数据进行插入和更新。要求在listcontrol里面能实时显示当前的实况信息,也就是说,在别的类里面怎样插入和更新listcontrol数据。
麻烦精通的认识给出源码,感激不尽。

解决方案 »

  1.   

    CShiKuang写一个函数更新树,别的类调用。
      

  2.   

    其实就是在CRightListView里面的list里面选者一个用户,放在Ctemptablenew里面的list里面
    BOOL Ctemptablenew::OnInitDialog() 
    {
    CDialog::OnInitDialog();

    CRect rect;
    m_listCtrl.GetClientRect(&rect);
    //设置列表控件风格
    DWORD dwStyle=::GetWindowLong(m_hWnd,GWL_STYLE);
            dwStyle|=LVS_REPORT|LVS_SHOWSELALWAYS|LVS_EDITLABELS;
    ::SetWindowLong(m_hWnd,GWL_STYLE,dwStyle);
    dwStyle=m_listCtrl.GetExtendedStyle();
    dwStyle|=LVS_EX_FULLROWSELECT;//LVS_EX_GRIDLINES|
        //设置扩展风格
    m_listCtrl.SetExtendedStyle(dwStyle);
    m_listCtrl.InsertColumn(0, "姓名", LVCFMT_CENTER, rect.Width()/8);
    m_listCtrl.InsertColumn(1, "大单位", LVCFMT_CENTER, 0);//后面使用,不显示,所以宽度为0
    m_listCtrl.InsertColumn(2, "驻地", LVCFMT_CENTER,0);
        m_listCtrl.InsertColumn(3, "性别", LVCFMT_CENTER,rect.Width()/12);
    m_listCtrl.InsertColumn(4, "出生年月", LVCFMT_CENTER,rect.Width()/7);
    m_listCtrl.InsertColumn(5, "专业方向", LVCFMT_CENTER,rect.Width()/7);
    m_listCtrl.InsertColumn(6, "单位", LVCFMT_CENTER,rect.Width()/4);
    m_listCtrl.InsertColumn(7, "职务", LVCFMT_CENTER,rect.Width()/8);
    m_listCtrl.InsertColumn(8, "联系方式", LVCFMT_CENTER,rect.Width()/8);
    updata(strName,strFirClass,strSecClass);

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
    }///这是更新函数
    void Ctemptablenew::updata(CString strName,CString strFirClass,CString strSecClass)
    {

             CString strSQL;
    CString str, sex,birth,fangxang,buzhibie,zhiji,lianxifangshi;
    strSQL.Format("select * from info where 姓名='%s' \
    and 一级类别='%s' and 二级类别='%s'",\
    strName,strFirClass,strSecClass);//3个为全局变量
    //打开记录集 选择表名
    if(!OpenRecordSet(m_pRecordset,strSQL))
    {
    AfxMessageBox("没有成功打开数据表");
    return;
    }

    str=pLeftView->VariantToCString(m_pRecordset->GetCollect("姓名"));
    sex=pLeftView->VariantToCString(m_pRecordset->GetCollect("性别"));
    birth=pLeftView->VariantToCString(m_pRecordset->GetCollect("出生日期"));
    birth=birth.Mid(0,10);//把恶心的时分秒去掉,嘿嘿
    fangxang=pLeftView->VariantToCString(m_pRecordset->GetCollect("心理工作特长"));
    buzhibie=pLeftView->VariantToCString(m_pRecordset->GetCollect("单位"));
    zhiji=pLeftView->VariantToCString(m_pRecordset->GetCollect("职务"));
    lianxifangshi=pLeftView->VariantToCString(m_pRecordset->GetCollect("联系方式"));
    m_listCtrl.SetItemText(i, 0, strName);
            m_listCtrl.SetItemText(i, 1, strFirClass);
    m_listCtrl.SetItemText(i, 2, strSecClass);
    m_listCtrl.SetItemText(i, 3, sex);
    m_listCtrl.SetItemText(i, 4, birth);
    m_listCtrl.SetItemText(i, 5, fangxang);
    m_listCtrl.SetItemText(i, 6, buzhibie);
    m_listCtrl.SetItemText(i, 7, zhiji);
    m_listCtrl.SetItemText(i, 8, lianxifangshi);
    i++;

    }这是在另外一个类里更新
    void CRightListView::OntoExpertTable() 
    {
    // Ctemptable *ptable=new Ctemptable;
    POSITION pos=m_listCtrl.GetFirstSelectedItemPosition();
    int iIndex=m_listCtrl.GetNextSelectedItem(pos); if(iIndex==-1)
    {
    return;
    }
    //以下三个是全局变量,在Ctemptablenew类里面引用
    strName=m_listCtrl.GetItemText(iIndex,0);
    strFirClass=m_listCtrl.GetItemText(iIndex,1);
    strSecClass=m_listCtrl.GetItemText(iIndex,2);


            Ctemptablenew *ptable=new Ctemptablenew; if (m_pDlg==NULL)//m_pDlg都是全局变量,标记是否创建窗口,如果没有的就创建
    {
       
                    m_pDlg=ptable;
    CRect r;
    GetWindowRect(&r);
    ScreenToClient(&r);
    r.left=r.left+r.Width()/3;
    r.right=r.right+r.Width()/3;
    r.top=r.top+230;
    r.bottom=r.bottom+120;
    ptable->Create(IDD_DIALOG1,this);
    ptable->MoveWindow(r);
    ptable->ShowWindow(SW_SHOW);
    //////////////////////////////////////////加入第一个选中的人 //第一个挑选的,随着窗口一起被创建 }
    else
    {

               ptable->updata(strName,strFirClass,strSecClass);

    }
    ptable->UpdateData(FALSE);

    }