我在一个对话框中,有一个CListCtrl控件,用来显示从数据库中查出的数据
还有一个按钮
当第一次点击按钮时,修改CListCtrl控件中一条记录的第一个字段,如在这个数据后加一个字
当第二次点击按钮时,将刚加的那条记录的那个字段的字去掉参考大家的意见,我的思路是这样的
首先定义一个bool型变量m_bFirst
然后在NM_CKICK事件中初始化m_bFirst=true;
再在按钮的响应事件里里进行判断
if(m_bFirst)
{
第一次操作
}
else
{
第二次操作
}
m_bFirst = !m_bFirst;
}但是,可但是,结果还是不正确,这个问题已经困扰我好久了,希望大家 帮帮忙,看看是不是哪的想法不对,尤其是CListCtrl中添加NM_CLICK事件中m_bFirst=true;这句,我觉得是对的
分不多了,一点心意,已经是全部家当了

解决方案 »

  1.   

    void CStudentDlg::OnGiveButton() 
    {
    // TODO: Add your control notification handler code here
    if(m_bFirst)
    {
    CString strStudentNo;
    int i = m_lstStudentInfo.GetCurSel();
    if(-1 == i)
    MessageBox(_T("请选择菜肴!"));
    else
    {
    if(IDOK==MessageBox(_T("是否操作"),_T("提示信息:"),MB_OKCANCEL))
    {
    strStudentNo = m_lstStudentInfo.GetItemText(i,2);
    theApp.m_gstrStudentNo = strStudentNo;
    if(ModifyMark(strStudentNo,_T("10")))
    ShowAll();
    else
    MessageBox(_T("操作失败"));
    }
    }
    }
    else
    {
    CString strStudentNo;
    int i = m_lstStudentInfo.GetCurSel();
    if(-1 ==i )
    MessageBox(_T("请选择!"));
    else
    {
    strStudentNo = m_lstStudentInfo.GetItemText(i,2);
    theApp.m_gstrStudentNo = strStudentNo;
    if(ModifyMark(strStudentNo,_T("00")))
    ShowAll();
    else
    MessageBox(_T("已修改"));
    } }
    m_bFirst =!m_bFirst;
    }
     void CStudentDlg::OnItemchangedStudentinfoList(NMHDR* pNMHDR, LRESULT* pResult) 
     {
      NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
       // TODO: Add your control notification handler code here
       if(pNMListView ->iItem == -1)
      {
       return ;
       }
       m_bFirst = true; //感觉这里不太对,帮忙分析一下
      *pResult = 0;
     }
    void CStudentDlg::ShowAll()
    {
    UpdateData(TRUE); Invalidate(false);
    m_strSQL.Format(_T("SELECT StudentName,Mark,StudentNo FROM StudentInfo WHERE StudentNo ='%s'"),theApp.m_gstrStudentNo);
    CVORecordset rs(theApp.m_Conn);

    rs.Open(m_strSQL,adOpenForwardOnly,adLockReadOnly);
    m_lstStudentInfo.DeleteAllItems();

    while(m_lstStudentInfo.DeleteColumn(0)); if(!rs.IsOpen())
    AfxMessageBox(_T("内部错误"));
    else
    {
    rs.MoveFirst();
    if(!rs.IsEOF()) // 有记录
    {
    int iNew;
    CString strFieldName;
    CString strFieldValue;

    for(int i=0; i<rs.GetFieldCount(); i++)
    {
    strFieldName=rs.GetFieldName(i);
    m_lstStudentInfo.InsertColumn(-1,strFieldName);
    }
    int j =0;
    CString strTmp;
    while(!rs.IsEOF())
    {
    if(rs.GetFieldCount()<1)
    break;

    strFieldValue = rs.GetFieldValueString(0);
    m_lstStudentInfo.InsertItem(-1,strFieldValue);
    m_lstStudentInfo.SetItemText(j,0,strFieldValue);   
    strFieldValue = rs.GetFieldValueString(1);
    m_lstStudentInfo.SetItemText(j,2,strFieldValue);
    if(strFieldValue == "10")         //要不就是这里,晕了,就是搞不定
    {
    strTmp = "团";
    m_lstStudentInfo.SetItemText(j,1,strTmp);
    }
    else
    {
    m_lstStudentInfo.SetItemText(j,1,rs.GetFieldValueString(1));
    } strFieldValue = rs.GetFieldValueString(2);
    m_lstStudentInfo.SetItemText(j,2,strFieldValue);

    j++;
    rs.MoveNext();
    }
    TRACE(_T("Field Count:%d\n"),rs.GetFieldCount());
    }
    }
    m_lstStudentInfo.SetFocus();
    m_lstStudentInfo.SetCurSel(((int)rs.GetRecordCount()-1)); rs.Close();
    Invalidate(true);
    }
    谢谢,全靠大家了
      

  2.   

    用两个按钮的话,结果就对了,可是我想用一个
    我觉得主要的问题在于选择LISTCTRL中某一项时m_bFist的值不对
    大家 觉得呢
      

  3.   

    然后在NM_CKICK事件中初始化m_bFirst=true;///////////////////////////////////////那是什么事件?把m_bFirst=true;放到初始化INITDIG里
      

  4.   

    首先定义一个bool型变量m_bFirst
    然后在WM_INITDIG事件中初始化m_bFirst=true;
    再在按钮的响应事件里里进行判断
    if(m_bFirst)
    {
    第一次操作;
    m_bFirst=false;
    }
    else
    {
    第二次操作
    }
    m_bFirst = true;
    }
      

  5.   

    首先定义一个bool型变量m_bFirst
    然后在WM_INITDIG事件中初始化m_bFirst=true;
    再在按钮的响应事件里里进行判断
    if(m_bFirst)
    {
    第一次操作;
    m_bFirst=false;
    }
    else
    {
    第二次操作;
    m_bFirst = true;
    }
      

  6.   

    比如现在处于List第一条,点了一下按钮,第一条加了几个字,然后移动到第二条,点一下按钮,你是要去掉第一条加的字,还是给第二条加上字呢?
    看起来不像是前者,因为当前操作列你是用GetCurSel取得的,你根本不知道上次位于那一行,如果是后者,那么应该有几行就要有几个标记位。
    个人认为LZ的设计有些问题……
      

  7.   

    比如现在处于List第一条,点了一下按钮,第一条加了几个字,然后移动到第二条,点一下按钮,你是要去掉第一条加的字,还是给第二条加上字呢?
    先回答这个问题……
      

  8.   

    那只有两种方法:
    1、给每一条都用一个bool变量作标记,可以用一个vector保存这些标记
    2、直接读取每一条,判断是否有这几个字,决定删除或添加。另外再让“m_bFirst = true; //感觉这里不太对,帮忙分析一下”这一行见鬼,应该就差不多了