Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1123
  • Last Modified:

Treeview with LoadFromStream

===============================
    a:=tstringlist.create;
    a.LoadFromFile(treename);
    for i:=0 to a.Count-1 do begin
        stemp:=a.Strings[i];
        if pos('====',stemp)=1 then begin
            for j:=i to a.count-1 do
                a.Delete(a.Count-1);
            goto oki;
        end;//if
    end;//for
    oki:
    st:=tstream.Create;
    a.SaveToStream(st);
    st.Position:=0;
    tree.LoadFromStream(st);
    a.Free;
    st.Free;
==============================
1. "treename" is a file saved as treeview1.savetofile(...)
2. after treename is saved, there is more text added in the tree file, at the end of the file
3. the code above tryed to load the info back in the tree, ignoring the lines written after "=========" line (those lines are deleted)
4. the ideea is :
   a. load the entire file into a tstring object
   b. delete the lines after "======" line
   c. copy the remanis into a stream
   d. load from stream into treeview

Something is wrong in my code. I get an "abstract...." error. help.
0
Catalin80
Asked:
Catalin80
  • 2
1 Solution
 
KalrothCommented:
When you create the stream ("st" variable), use "tmemorystream" instead of "tstream", that's why you get an abstract error.

Instead of "goto oki;" just use "break", using goto gives me the shivers. :)
0
 
KalrothCommented:
This works fine for me.

(** CODE EXAMPLE **)

procedure TForm1.Button1Click(Sender: TObject);
var
  i, j: integer;
  stemp: string;
  a : tstringlist;
  st : tmemorystream;
begin
  a:=tstringlist.create;
  try
    a.LoadFromFile('treetest.txt');
    for i:=0 to a.Count-1 do
    begin
      stemp:=a.Strings[i];
      if pos('====',stemp)=1 then
      begin
        for j:=i to a.count-1 do
          a.Delete(a.Count-1);
        break;
      end;//if
    end;//for

    st:=tmemorystream.Create;
    try
      a.SaveToStream(st);
      st.Position:=0;
      tree.LoadFromStream(st);
    finally
      st.Free;
    end;
  finally
    a.Free;
  end;
end;

(** /CODE EXAMPLE **)
0
 
Catalin80Author Commented:
I AGREE YOU 100% :
1. using goto gives me too the shivers. I just was in a hurry.
2.
"When you create the stream ("st" variable), use "tmemorystream" instead of "tstream", that's why you get an abstract error."

I thought it was so. It's like tstrings <-> tstringlist. Just didn't knew what was the "other" object.

Thx. alot.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now