我在程序中用了一个treeview控件,他的node都要存到数据库中去,在数据库中有他的唯一代码,所以每次增加时我用的时item.addchildobject()并定义了一个指针
指针中有代码项,在展开时是对的,但是,在 chang时得出的代码是最后一个加入的代吗,这是怎么问题????

解决方案 »

  1.   

    PNodeData = ^NodeData;
          NodeData=record
          nodeCaption:string;
          nodeId:integer;
         end;-----------------
    function TGngz_Form.HasChildrenNode(nodeid:integer):boolean;
    var
     query1:TADOQuery;
    begin
    query1:=TADOQuery.Create(self);
    query1.Connection:=dm.ADOConnection1;
    with query1 do
       begin
       close;
       sql.Clear;
       sql.Add('select * from tree where ParentID=:nodeid');
       parameters.ParamByName('nodeid').Value:=nodeid;
       prepared;
       open ;
       if recordcount<>0 then
        result:=true
        else
        result:=false;
        close;
        end;
    query1.Free;
    end;
    procedure TGngz_Form.TreeView1Expanding(Sender: TObject; Node: TTreeNode;  //-展开
      var AllowExpansion: Boolean);var
    nodeid:integer;
    nodeptr:PnodeData;
    nodeptr1:PnodeData;
    adoquery_treeNode:Tadoquery;
    childnode:TTreeNode;
    itemstr:string;
    i:integer;
    hasChild:Boolean;
    begin
    adoquery_treeNode:=Tadoquery.Create(self) ;
    adoquery_treeNode.Connection:=dm.ADOConnection1;
    nodeptr:=node.Data ;
    nodeid:=nodeptr.nodeId;
    showmessage(inttostr(nodeid));
    for i:=0 to node.Count-1 do
       node.DeleteChildren;
    with adoquery_treenode do
       begin
        close;
        sql.Clear;
        sql.Add('select * from tree where parentId=:nodeid and nodeAtt=1');
        parameters.ParamByName('nodeid').Value:=nodeid;
        open;
        if recordcount<>0 then
         begin
         new(nodeptr1);
         first ;
         //showmessage(inttostr(recordcount));
         while not eof do
         begin
           itemstr:=trim(fieldbyname('nodeCaption').AsString);
           nodeptr1.nodeCaption:=itemstr;
           nodeptr1.nodeId:=fieldbyname('Nodeid').AsInteger;
           childNode:=treeview1.Items.AddChildObject(Node,itemstr,nodeptr1);
           childnode.ImageIndex:=2;
           childnode.SelectedIndex:=4;
           hasChild:=HasChildrenNode(nodeptr1.nodeId);
           if hasChild=true then
             begin
             childNode.HasChildren:=true;
             //childnode.Selected;
             end
           else
             childNode.HasChildren:=false;
           next;
           end;
         end;
         close;
       end;
      // dispose(nodeptr1)
      end;
    procedure TGngz_Form.TreeView1Changing(Sender: TObject; Node: TTreeNode;//------
      var AllowChange: Boolean);
    var
    nodeptr:PNodeData;             //指针
    nodeid:integer;
    query_listnode:TAdoquery;
    //nIcon:integer;
    xm:string;                    //项目
    NAtt:integer;
    lb:string;                   //类型
    bh: string;                  //编号
    desc:string  ;            //描述
    ListItem: TListItem;
    id:integer;
    //i:integer;
    nodeptr1:pnodedata;
    begin
     nodeptr:=node.Data;;
     nodeid:=nodeptr^.nodeId; //父节点编号
     //showmessage(nodeptr^.nodeCaption);
     query_listnode:=Tadoquery.Create(self);
     query_listnode.Connection:=dm.ADOConnection1;
     listview1.Items.Clear;
     with query_listnode do
       begin
        close;
        sql.Clear;
        sql.Add('select * from tree where ParentID=:nodeid');
        parameters.ParamByName('nodeid').Value:=nodeid;
        prepared;
        open ;
        if recordcount<>0 then
         begin
         //setlength(listitemarray,recordcount);
         first;
         new(nodeptr1);
          //i:=1;
         while not eof do
            begin
              id:=fieldbyname('Nodeid').AsInteger;
              xm:=trim(fieldbyname('NodeCaption').AsString);
              bh:=trim(fieldbyname('DemandNo').AsString);
              nodeptr1.nodeCaption:=xm;
              nodeptr1.nodeId:=id;
              //listitemarray.nodeCaption:=xm;
              //listitemarray.nodeId:=id;
              desc:=trim(fieldbyname('NodeDesc').AsString);
              //nIcon:=fieldbyname('IconNo').AsInteger;
              NAtt:=fieldbyname('NodeAtt').AsInteger;
              if NAtt=1 then
                 lb:='项目'
               else
                 lb:='项目组' ;         listitem:=listview1.Items.Add;
             listitem.Data:=nodeptr1;
             listitem.Caption:=xm;
             listitem.SubItems.Add(bh);
             listitem.SubItems.Add(lb);
             listitem.SubItems.Add(desc);
             //i:=i+1   ;
             next ;
            end;
          end;
         end;
     end;
      

  2.   

    每添加一个结点,相当于把该结点和这个指针关联起来
    你只定义了一个指针变量nodeptr1
    最后所有的结点都指向同一个指针
    它的值就是循环最后的那个值定义个指针数组试试