BCB中ListView控件selected和checked的区别
程序要求统计ListView中选中项的数目,程序大致如下:
POINT pos;
pos.x = Mouse->CursorPos.x;
pos.y = Mouse->CursorPos.y;
::ScreenToClient(ListView->Handle,&pos);
TListItem* Item = ListView->GetItemAt(pos.x,pos.y);
bSelected = Item->Selected;
bool bChecked = Item->Checked;
if(bChecked && !bSelected)
m_SelectNum ++;
else if(!bChecked && !bSelected)
m_SelectNum --;
else if(bSelected && bChecked){
m_SelectNum --;
Item->Checked = false;
Item->Selected = false;
}
else if(bSelected && !bChecked){
m_SelectNum ++;
Item->Checked = true;
Item->Selected = false;
}
可是程序运行的,当以选中的项再次点击取消选中时,m_SelectNum的值就会出现问题,甚至多次点击一项后会出现负数,请各位大牛给指点一下!
[解决办法]
Selected是ListView当前选中的记录。而Checked为true则表示当前记录前面的复选框被选中。
[解决办法]
给你一个简单例子:
void __fastcall TfrmTest::ListView1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TListItem *li = ListView1->GetItemAt(X, Y);
THitTests ht = ListView1->GetHitTestInfoAt(X, Y);
if (li && ht.Contains(htOnStateIcon))
li->Checked? m_SelectNum++: m_SelectNum--;
Caption = m_SelectNum;
}