Quantcast
Channel: Embarcadero Community - Embarcadero Community
Viewing all articles
Browse latest Browse all 3212

ListView items exchange problem

$
0
0

This question asked in stack overflow but its seems this problem very hard to answer  , I am trying to exchange items inside a list view Here is the procedure that I am trying to use

procedure ExchangeItems(lv: TListView; Const ItemFrom, ItemTo: Integer);
var
    Source, Target: TListItem;
begin
    lv.Items.BeginUpdate;

    try
        Source := lv.Items[ItemFrom];
        Target := lv.Items.Insert(ItemTo);
        Target.Assign(Source);
        Source.Free;
    finally
        lv.Items.EndUpdate
    end;
end;



it exchanges an empty item that does not exist in my listview and raise this exception

 First chance exception at $75164B32. Exception class EConvertError with message 'Cannot assign a nil to a TListItem'.

 

That's how I exchange items if some condition is met

 

procedure TFORM1.UpdateVisual(Sender: TObject; AUserData: Pointer; var AImage: TGifImage);
var
 Item: TListItem;
 i : integer;
begin
 Item := TListItem(AUserData);

  if ListView1.Items.IndexOf(Item) = -1 then
    Exit;

  Item.Data := AImage;
  AImage := nil;
// if recorder is active then select item that equal to ClientID and exchange it to Top
if recorder = True then
      begin
       for i := 0 to ListView1.Items.Count-1
     do if ListView1.Items[i].SubItems[3] = IntToStr(ClientID)
     then
     begin
     ExchangeItems(ListView1, Item.Index, 0);
     ListView1.Invalidate;
     end;
   end;
end;

 

 

 

How can I handle this exception?

 


Viewing all articles
Browse latest Browse all 3212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>