以下是我的单击事件代码:
void COnOffListViewDlg::OnClickOnoffList(NMHDR* pNMHDR, LRESULT* pResult) 
{
// TODO: Add your control notification handler code here
NMLISTVIEW* pNMListView=(NMLISTVIEW*)pNMHDR;
int nRow=pNMListView->iItem;
int nCol=pNMListView->iSubItem;
if (nCol==TOTAL_COLUMN-1)
{
int res=actionDlg.DoModal();
if (res==IDOK)
{
onOffList->SetItemText(nRow,nCol,theApp.strAction);
}
}
*pResult = 0;
}
无论怎么单击,nRow总是-1,不知道怎么回事?
跟List的风格有没有关系,我设置的List风格为:
onOffList->SetExtendedStyle(LVS_EX_GRIDLINES);

解决方案 »

  1.   

    我好像以前也遇到过,不记得怎么解决的了,不过如果改用GetFirstSelectedItemPosition和GetNextSelectedItem取肯定没问题
      

  2.   

    to guo_wei:
    能不能说具体点
      

  3.   

    DWORD dwPos = GetMessagePos();
    POINTS points;
    points = MAKEPOINTS(dwPos);
    CPoint point ;
    point.x = points.x;
    point.y = points.y;
    m_listProduct.ScreenToClient(&point);
    LVHITTESTINFO lvh;
    ZeroMemory(&lvh,sizeof LVHITTESTINFO );
    lvh.pt = point;
    //m_listProduct.HitTest(&lvh);
    m_listProduct.SendMessage(LVM_SUBITEMHITTEST ,(WPARAM)0,(LPARAM)&lvh);
    if(lvh.iItem < 0)return; if(lvh.iSubItem == 4)//修改出库数量。
    {
    CModifyNumDlg dlg;
    CString str = m_listProduct.GetItemText(lvh.iItem,4);
    dlg.m_iNum = atoi(str);
    if(dlg.DoModal() == IDOK)
    {
    str.Format("%d",dlg.m_iNum);
    m_listProduct.SetItemText(lvh.iItem,4,str);
    } }
    if(lvh.iSubItem == 7)//修改生产车间。
    {
    CSelectDepartDlg dlg;
    if(dlg.DoModal() == IDOK)
    {
    m_listProduct.SetItemText(lvh.iItem,7,dlg.m_strDepartName);
    m_listProduct.SetItemText(lvh.iItem,1,dlg.m_strDepartID);
    }
    } *pResult = 0;
      

  4.   

    POSITION ps;
    int nIndex;
    ps = m_List.GetFirstSelectedItemPosition();
    nIndex = m_List.GetNextSelectedItem(ps);
    if(nIndex == -1) //没有选择
    return;nIndex就是第一个选择的行索引
      

  5.   

    to # zzz3265
    按你的代码,行号显示还是-1
    不知道问题出在哪儿了
      

  6.   

    ...item是空的 行号当然是-1
      

  7.   


    告诉你一个简单的方法:1、先手动加入一、二条数据,用 InsertItem ,不要通过数据库加2、测试单击事件,看看返回值多少?有时候,好几个问题混在一起,就很难解决;所以,可以先把问题简单话,一个一个独立解决,然后再集成起来。
      

  8.   

    int index=m_cList.GetSelectionMark( );
    CString cardid=m_cList.GetItemText(index,0);
      

  9.   

    lz你的nRow值本就应该是-1。
    msdn上有:
    iItem 
    Identifier of the list-view item, or -1 if not used. 
    也就是说nRow不是你想的表示某个listitem,而是"list-view的identifier"
      

  10.   

    nRow总是-1,是因为没有数据吧
      

  11.   

    POSITION ps; 
    int nIndex; 
    ps = m_List.GetFirstSelectedItemPosition(); 
    if(!pos)
       return;
    nIndex = m_List.GetNextSelectedItem(ps); 
    先要判断有没有选中项目,再获取被选中的项目索引号。
      

  12.   

    问题的症结在于要设置List的style,设置满行选择(fullselected)就可以了,最后问题解决了,还是谢谢各位的帮助。