Moving items in a TListView with drag and drop


2004-01-26 03:14:11 AM
cppbuilder73
I want to give the user the opportunity to move objects in a TListView
with Drag & Drop.
The TListView (lvAula) can be shown both as vsIcons and vsReport
(through a button in the toolbar).
vsReport mode shows additional object's information in columns
MoveItem is called by DragOver after some checks
fromidx and toidx are right.
In vsReport mode it works fine. Icons (and objects) follow mouse drag
and drop operations.
The same code in vsIcon mode doesn't work. Icons keep their position.
Sometimes icons are moved
to the end of the list.
void __fastcall TMyFrame::MoveItem(int fromidx,int toidx)
{
bool selected,focused;
TListItems * items=lvAula->Items;
if (((fromidx < 0) || (fromidx>items->Count)) ||
((toidx < 0) || (toidx>items->Count)) || (fromidx==toidx))
return;
items->BeginUpdate();
try
{
TListItem * newitem;
selected = items->Item[fromidx]->Selected;
focused = items->Item[fromidx]->Focused;
if (toidx>fromidx)
{
newitem = items->Insert(toidx + 1);
newitem->Assign(items->Item[fromidx]);
items->Delete(fromidx);
}
else
{
newitem = items->Insert(toidx);
newitem->Assign(items->Item[fromidx+1]);
items->Delete(fromidx+1);
}
newitem->Selected = selected;
newitem->Focused = focused;
}
__finally
{
items->EndUpdate();
}
}
Any help will be really appreciated.
Thanks.