我的TreeView中的好几层, 我现在只要得到第一层的所有名字?怎么做,越简单越好。

解决方案 »

  1.   

    这是我以前用来创建时间(年月日)的模板树:
    //模板树的创建
    procedure TfrmZxyOperationView1.tvLeftCreate;
    var
      nodeRoot,nodeLevel1,nodeLevel2,nodeLevel3:Ttreenode;
      strSQL,strWhere:string;
      i:integer;
    begin
      tvLeft.Items.Clear;
      //level 0
      nodeRoot:=tvLeft.Items.Add(nil,TxtNodeRoot);
      nodeRoot.ImageIndex:=intTvImgIndex;
      nodeRoot.SelectedIndex:=intTvSelectedIndex;  strWhere:='1=1';
      strSQL:=format(strSqlFormat,[fKeyField,'2',fMasterTb,strWhere]);//'select DISTINCT left(fKeyField,4) from td_InBill'
      //msgbox(strSQL);
      //level 1
      with frmPcManagerData.Qry1 do
      begin
        close;
        sql.Clear;
        sql.Add(strSQL);
        open;
        while not eof do //w1
        begin
          new(fNodePointer1);
          fNodePointer1.NodeID:=fields[0].AsString;//03
          fNodePointer1.NodeName:='20'+fNodePointer1.NodeID+'年';
          nodeLevel1:=tvLeft.Items.AddChildObject(nodeRoot,fNodePointer1.NodeName,fNodePointer1);
          nodeLevel1.ImageIndex:=intTvImgIndex;
          nodeLevel1.SelectedIndex:=intTvSelectedIndex;
          //level 2
          with frmPcManagerData.Qry2 do
          begin
            strWhere:=format(strWhereFormat,[fKeyField,'2',fNodePointer1.NodeID]);
            strSQL:=format(strSqlFormat,[fKeyField,'4',fMasterTb,strWhere]);
           // msgbox(strSQL);
            close;
            sql.Clear;
            sql.Add(strSQL);
            //msgbox(sql.Text);
            open;
            while not eof do//w2
            begin
              new(fNodePointer2);
              fNodePointer2.NodeID:=fields[0].AsString;//0306
              fNodePointer2.NodeName:=copy(fNodePointer2.NodeID,3,2)+'月';
              nodeLevel2:=tvLeft.Items.AddChildObject(nodeLevel1,fNodePointer2.NodeName,fNodePointer2);
              nodeLevel2.ImageIndex:=intTvImgIndex;
              nodeLevel2.SelectedIndex:=intTvSelectedIndex;
              //level 3
              with frmPcManagerData.Qry3 do
              begin
                strWhere:=format(strWhereFormat,[fKeyField,'4',fNodePointer2.NodeID]);
                strSQL:=format(strSqlFormat,[fKeyField,'6',fMasterTb,strWhere]);
                //msgbox(strSQL);
                close;
                sql.Clear;
                sql.Add(strsql);
                //msgbox(sql.Text);
                open;
                while not eof do//w2
                begin
                  new(fNodePointer3);
                  fNodePointer3.NodeID:=fields[0].AsString;//030618
                  fNodePointer3.NodeName:=copy(fNodePointer3.NodeID,5,2)+'日';
                  nodeLevel3:=tvLeft.Items.AddChildObject(nodeLevel2,fNodePointer3.NodeName,fNodePointer3);
                  nodeLevel3.ImageIndex:=intTvImgIndex;
                  nodeLevel3.SelectedIndex:=intTvSelectedIndex;
                  next//w3
                end;
                close;
              end;
              next;//w2
            end;
            close;
          end;
          next;//w1
        end;
        close;
      end;
      with tvLeft do
      begin
      FullExpand;
      if Items.Count<3 then exit;
      for i:=0 to Items[0].Item[0].Count-1 do
        Items[0].Item[0].Item[i].Collapse(false);
      end;
    end;
      

  2.   

    这是我以前的全树搜索(根据结点ID找到树中对应结点),
    {这里写全树搜索模板}
    procedure TfrmZxyOperationView1.locateNode(aNodeID:string);
    var
      TempNode:TTreeNode;
    begin
      tvLeft.SetFocus;
      TempNode:=tvLeft.Items.GetFirstNode;
      while (TempNode <> nil)  do
      begin
        TempNode:=TempNode.getNext;
        if TempNode.Level<>3 then continue; //不等于第三层则继续
        if PMyNode(TempNode.Data)^.NodeID=aNodeID then break;//找到了则停止
      end;
      TempNode.Parent.Expand(true);
      TempNode.Selected:=true;
    end;