就像PB的TreeView一样还有一个Data属性

解决方案 »

  1.   

    TreeNode也有Data属性type
    PMyRec = ^TMyRec;
    TMyRec = record
      FName: string;
      LName: string;
    end;procedure TForm1.Button1Click(Sender: TObject);var
      MyRecPtr: PMyRec;
      TreeViewIndex: LongInt;
    begin
      New(MyRecPtr);
      MyRecPtr^.FName := Edit1.Text;
      MyRecPtr^.LName := Edit2.Text;
      TreeViewIndex := StrToInt(Edit3.Text);
      with TreeView1 do
      begin
        if Items.Count = 0 then
          Items.AddObject(nil, 'Item' + IntToStr(TreeViewIndex), MyRecPtr)
        elseif (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then
          Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);  end;
    end;procedure TForm1.Button2Click(Sender: TObject);begin
      Label1.Caption := PMyRec(TreeView1.Selected.Data)^.FName + ' ' +
                      PMyRec(TreeView1.Selected.Data)^.LName;
    end;
      

  2.   

    我一般使用Node的ImageIndex或者StateIndex属性,一般是StateIndex,因为在TreeView中一般有Image就行了,很少使用了StateImage的,因此我使用StateIndex来保存一些额外的数据,比如数据库中记录的ID等等。