Board index » cppbuilder » When dragging and dropping

When dragging and dropping


2003-10-11 06:54:34 AM
cppbuilder97
When draggin and dropping from a ListView, what direction is your loop
supposed to go in? The way i am doing my loop is
for(int x = ListView1->Items->Count-1; x>= 0; --x)
{
if(ListView1->Items->Item[x]->Selected)
{
// Insert the new record into another Table
..... code....
// delete the record from the DB and TList
tbl1->Delete();
ListView1->Items->Item[x]->Delete();
delete((TMsgInfo*)aList->Items[x]);
aList->Delete(x);
}
}
Now if i select one item and i drag and drop it, that works fine. But
if i select two or more items, it inserts the first record into the
db, then when it gets to the second record it crashes. I am assuming
that when i delete the record the index numbers are different, and
thats what's causing the crash because its not finding the correct
record anymore. If i comment out the code that deletes anything, it
does not crash and the records are copied to the necessary tables.
Whats strange is, i created a sample project with the necessary code
to drag and drop from one table to another (same code). And i can
select a multiple number of records, and i never get a crash when
looping through and deleting the records. At first i was thinking
there was a global var being duped somewhere. But this function does
not rely on any of those, other than that i am lost.
 
 

Re:When dragging and dropping

"Greg Stantin" < XXXX@XXXXX.COM >wrote in message
Quote
When draggin and dropping from a ListView, what
direction is your loop supposed to go in?
Since you are deleting ListView items inside the loop, you need to loop
backwards, as you already are.
Quote
delete((TMsgInfo*)aList->Items[x]);
aList->Delete(x);
What is aList, and how is it filled? Are you sure that the number of
elements in the list always matches the number of ListView items? If you're
using the list to keep track of information that belongs to the individual
TListItem, have you considered using the TListItem::Data property instead of
a separate list?
Quote
Now if i select one item and i drag and drop it, that works
ne. But if i select two or more items, it inserts the first record
to the db, then when it gets to the second record it crashes.
Please be more specific. Crashes how exactly? On which line?
Quote
am assuming that when i delete the record the index numbers
are different
Thus the need to loop backwards so that you don't mess up the loop's counter
on each deletion.
Gambit
 

Re:When dragging and dropping

The code goes like this
for(int x = ListView1->Items->Count-1; x>= 0; --x)
{
if(ListView1->Items->Item[x]->Selected)
{
TVarRec vr = (int)(ListView1->Items->Item[x]->Data);
if(tblMaster->FindKey(&vr, 0))
{
tblDetail->Insert();
// get the ID from the currently inserted message
int ParentID =
tblDetail->FieldByName("ID")->AsInteger;
// MessageID field
tblDetail->FieldByName("RecID")->AsString =
tblMaster->FieldByName("RecID")->AsString;
------------------- snip
tblDetail->Post();
tblMaster->Delete();
ListView1->Items->Item[x]->Delete();
delete((TMsgInfo*)aList->Items[x]);
aList->Delete(x);
}
}
}
Quote
Please be more specific. Crashes how exactly? On which line?
It runs through fine on the first record to be inserted into the db,
but when it starts
the second record it crashes on this line.
tblDetail->FieldByName("RecID")->AsString =
tblMaster->FieldByName("RecID")->AsString;
Error: tblDetail ->Field RecID not found
Not sure at all why i get this error, all of the fields exist
Quote
What is aList, and how is it filled? Are you sure that the number
of
elements in the list always matches the number of ListView items?
If you're
using the list to keep track of information that belongs to the
individual
I am using the TList to store the items i am inserting into the
Virtual ListView
TMsgInfo* mi = (TMsgInfo*)aList->Items[Item->Index];
Item->Caption = mi->Name;
Item->SubItems->Add(mi->From);
Item->Data = (void*)mi->ID;
The items in the list corresponds with whats in the ListView at the
time
 

{smallsort}

Re:When dragging and dropping

"Greg Stantin" < XXXX@XXXXX.COM >wrote in message
Quote
I am using the TList to store the items i am inserting
into the Virtual ListView
Since you are using a virtual ListView, *do not* call TListItem::Delete() at
all. Delete() only needs to be used with *non* virtual ListViews. To
remove items from a virtual ListView, you only need to remove the items from
your data list and then update the Items->Count, nothing else. With that
said, I would suggest you re-write your code to something more like the
following instead (untested):
// using an array to store the indexes of items to remove so that the
Detail
// table can insert records in the same order that they were in the the
Master.
// Plus it makes the searching for selected ListItems easier to manage
if the
// loop can move forward instead of backward.
TListItem *Item = ListView1->Selected;
if( Item )
{
DynamicArray<int>IndexesToDelete;
IndexesToDelete.Length = ListView1->SelCount;
int NumToDelete = 0;
try
{
while( Item )
{
TVarRec vr = (int)(Item->Data);
if( tblMaster->FindKey(&vr, 0) )
{
tblDetail->Insert();
//...
tblDetail->Post();
tblMaster->Delete();
IndexesToDelete[NumToDelete++] = Item->Index;
}
Item = ListView1->GetNextItem(Item, sdAll, TItemStates() <<
isSelected);
}
}
__finally
{
// just in case an exception occured above
IndexesToDelete.Length = NumToDelete;
for(int i = NumToDelete-1; i>= 0; --i)
{
delete (TMsgInfo*)aList->Items[IndexesToDelete[i]];
aList->Delete(IndexesToDelete[i]);
}
ListView1->Items->Count = aList->Count;
}
}
Gambit
 

Re:When dragging and dropping

Quote
Since you are using a virtual ListView, *do not* call
TListItem::Delete() at
all. Delete() only needs to be used with *non* virtual ListViews.
To
remove items from a virtual ListView, you only need to remove the
items from
your data list and then update the Items->Count, nothing else. With
that
Thanks, i definitely did not think of this at all, but as you
mentioned it
i am beginning to see what i was doing wrong with the Delete();
Appreciate the sample, i am going to put it to work right away.
Greg