/MULT/A
/MULT/B
/MULT/P
/MULT/U0/A
/MULT/U0/B
/MULT/U0/P
/MULT/U0/b2
如何根据TStringList创建树?

解决方案 »

  1.   

    stringlist可以按 '/'进行处理,然后进行一些判断生成tree
      

  2.   

    能给个小例子吗?初涉delphi,玩的不深。
      

  3.   

    procedure Button1Click(Sender: TObject);
      end;var
      Form1: TForm1;implementation{$R *.dfm}{函数的默认参数是指定显示在第几个元素下面}
    function DirToTree(Tree: TTreeView; Path: string; num: Integer = -1): Boolean;
    var
      sr: TSearchRec;
      node: TTreeNode;
    begin
      path := ExcludeTrailingPathDelimiter(path); {去掉最后一个 '\'}
      if not DirectoryExists(path) then Exit;     {路径不存在则退出}
      if num = -1 then node := nil else node := Tree.Items[num]; {确认节点}  if FindFirst(Path + '\*.*', faAnyFile, sr) = 0 then
      begin
        repeat
          if sr.Name[1] = '.' then Continue;      {如果是'.' 或 '..' (当前目录或上层目录)则忽略}      Tree.Items.AddChild(node, sr.Name);     {都是通过这句添加的}      Application.ProcessMessages;            {加上可以让程序兼顾其他消息}      {如果是文件夹则执行递归}
          if (sr.Attr and faDirectory) = faDirectory then
            DirToTree(Tree, Path + '\' + sr.Name, Tree.Items.Count-1);
        until (FindNext(sr) <> 0);
      end;
      Result := True;
    end;{测试}
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      TreeView1.Items.Clear;
      DirToTree(TreeView1, Edit1.Text);
    end;end.
      

  4.   

    这个不是查询文件的存储路径,有没有替换DirectoryExists和findfirst函数的呢
      

  5.   

    新人就帮一把,以后自己多研究一下,这个其实是个算法问题,以下代码测试通过unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls;type
      TForm1 = class(TForm)
        tv1: TTreeView;
        btn1: TButton;
        procedure btn1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;procedure SplitString(const source, ch: string; Results: TStrings);implementation{$R *.dfm}procedure SplitString(const source, ch: string; Results: TStrings);
    begin
      Results.CommaText := '"' + StringReplace(source, ch, '","', [rfReplaceAll]) + '"';
    end;procedure TForm1.btn1Click(Sender: TObject);
    var
      slSource, slSplit: TStringList;
      i, j, k: Integer;
      sText: string;
      ParentNode, ChildNode: TTreeNode;
    begin
      slSource := TStringList.Create;
      slSplit := TStringList.Create;
      try
        slSource.Add('/MULT/A');
        slSource.Add('/MULT/B');
        slSource.Add('/MULT/P');
        slSource.Add('/MULT/U0/A');
        slSource.Add('/MULT/U0/B');
        for i := 0 to slSource.Count - 1 do
        begin
          SplitString(slSource[i], '/', slSplit);
          ParentNode := nil;
          ChildNode := nil;
          for j := 0 to slSplit.Count - 1 do
          begin
            sText := slSplit[j];
            if Length(sText) = 0 then Continue;
            if ParentNode = nil then  //查找父级节点
            begin
              for k := 0 to tv1.Items.Count - 1 do
              begin
                if SameText(tv1.Items[k].Text, sText) then
                begin
                  ParentNode := tv1.Items[k];
                  Break;
                end;
              end;
              if ParentNode = nil then //没找到,则创建
                ParentNode := tv1.Items.AddChild(nil, sText);
            end
            else  //父级节点存在,则判断子节点中是否有重复的
            begin
              for k := 0 to ParentNode.Count - 1 do
              begin
                if SameText(ParentNode.Item[k].Text, sText) then
                begin
                  ChildNode := ParentNode.Item[k];
                  Break;
                end;
              end;
              if ChildNode = nil then //没找到,则创建
                ChildNode := tv1.Items.AddChild(ParentNode, sText);
              ParentNode := ChildNode;
              ChildNode := nil;
            end;
          end;
        end;
      finally
        slSource.Free;
        slSplit.Free;
      end;
    end;end.
      

  6.   

    多谢,后面大致相似,split 部分自己也写了一段。怎么给你加分?呵呵。。
    for i:=0 to LastList.Count-1 do begin
        ProgressChange('hehe', i / LastList.Count);      pathstring := LastList[i];
          node := nil;
          count := 0;
          while pathstring <> '' do begin
           p := PosEx('/', pathstring, 2);
            if p = 0 then
             p:=length(pathstring) + 1;
            nodestring := MidStr(pathstring,2,p-2);
            pathstring := MidStr(pathstring, p,99999999);