CListCtrl 添加新行后把新行设为选中高亮,删除某行,把某行下一行选中高亮

添加一行后,设为选中高亮

m_nindex = m_list.GetItemCount() -1 ;
m_list.SetItemState(m_nindex , LVIS_SELECTED, LVIS_SELECTED );

删除某行后,把某行的下一行选中高亮

void CDlgSysManager::OnBnClickedButtonDelete()
{
if ( m_list.GetItemCount() <2)
{
MessageBox( "can't delete last user", MB_OK );
return ;
}

DLGDeleteEnter dlg;
POSITION pos=m_list.GetFirstSelectedItemPosition();
if (pos)
{
if (IDOK == dlg.DoModal( ))
{
int index=m_list.GetNextSelectedItem(pos);//得到选中的行号,
m_nindex = index ;
if ( m_nindex == m_list.GetItemCount() -1)
{
m_nindex = index - 1;
}
deleteuser( index );

}
}
else
{
if (IDOK == dlg.DoModal( ))
{
int n = m_nindex;
if ( m_nindex == m_list.GetItemCount() - 1)//加入要删除的是最后一行,删除后最后一行行号要减1
{
m_nindex -= 1;
}
deleteuser( n );
}
}

void CDlgSysManager::deleteuser(int nindex)
{
CUser *pUser=( CUser * )m_list.GetItemData( nindex );//通过这个行号而取得内容的对象(指针);
if (pUser)
{
POSITION postemp=m_Usermap.Find(pUser);
if (postemp)
{
if (theApp.m_adoConn.ExecuteNoSelectSQL(pUser->GetDeleteSQL()))
{
m_Usermap.RemoveAt(postemp);
FillAllUerList();
ShowUserinfo();
}
}
}
delete pUser;
pUser = NULL;
m_list.SetItemState(m_nindex , LVIS_SELECTED, LVIS_SELECTED );
}

原文地址:https://www.cnblogs.com/chenzuoyou/p/3110653.html