delphivirtualtreeviewtvirtualstringtree

How not to add duplicate into TVirtualStringTree?


I have two VirtualStringTrees, first VST has been populated with data and I want to check the second VST and add nodes that are not already in the first VST. Or I want to add those nodes from second VST that are not duplicates of first VST.

procedure Tcreatevtform.copy2tosimvt(vt: Tvirtualstringtree);
var
  data: PMyRec;
  simvtdata: PMyRectF;
  rootnode, simvtnode: PVirtualNode;
  ty: string;
begin
  rootnode := vt.GetFirst; //vt is second virtualstringtree
  while Assigned(rootnode) do 
  begin
     data := vt.GetNodeData(rootnode);
     ty := data^.caption;
     if checksimduplicate(ty)=false then 
     begin
        simvtnode := similarvt.AddChild(nil); //similarvt is the first virtualstringtree
        simvtdata := similarvt.GetNodeData(simvtnode);
        simvtdata^.caption := data^.caption;
     end;
     rootnode := vt.GetNext(rootnode,false);
  end;
end;

function Tcreatevtform.checksimduplicate(t: string): boolean;
var
  data: PMyRectf;
  rootnode: PVirtualNode;
  typew: string;
begin
  Result := False;
  rootnode := similarvt.GetFirst;
  while Assigned(rootnode) do 
  begin
     data := similarvt.GetNodeData(rootnode);
     typew := data^.caption; // problem here, typew is always a constant or it is always the first
     if t=typew then 
     begin  
        // node's caption of vt (letter 'a' is the first node's caption in my
        // app. So this function is always false.
        Result := True;
        Break;
     end;
     similarvt.GetNext(rootnode, False);
  end;
end;

I am using D7.


Solution

  • Finding all items that are in one list but not a second can be done easily with a list comparison algorithm. Here's the basic idea:

    You can add actions to the equals case or either the first-unique or second-unique case to determine what to do in these cases. In your specific case, you'll want to make the second-unique case add an item to the VST. It might get more complicated if you have to preserve tree structure, but that's the basic idea.