Board index » delphi » HELP: Data items in TreeViews

HELP: Data items in TreeViews

My application has a TTreeView control that is central to its function. In that
control are various items that relate to items in a database. Each of these
items has at least one piece of data that I would like to store in the
TreeView with the items name. For example lets say that I want to place a list
of customers in the TreeView and each customer has a unique customer ID. The
customer ID is a long integer and I'd like to store that in the Data property
of the TTreeNode for that customer. However I'm having problems. I thought
something like this might work:

procedure TfrmMain.FormCreate(Sender: TObject);
var
   iIndex : integer;
   Company: TTreeNode;
   iCompanyID: ^LongInt;

begin

     Query1.Open;

     Query1.First;

     for iIndex := 0 to Query1.RecordCount -1 do
     begin

          Company := trvProjects.Items.Add(nil, Query1.Fields[0].AsString);
          iCompanyID^ := Query1.Fields[1].AsInteger;
          Company.Data := iCompanyID;

          Query1.Next;
     end;

end;

Where Query1 is a SQL query that returns the Company name in Field[0] and the
unique Company ID in Field[1], trvProjects is the TreeView component.

When I try this piece of code it works for the first iteration and then on the
second iteration, at the line "Company := trvProjects.Items.Add...", I get an
EAccessViolation error with the message:
"Access violation at addess 004165BB. Read of address FFFFFFFF".

Any help with this would be greatly appreciated.

TIA
Dale

 

Re:HELP: Data items in TreeViews


On 28 Mar 1997 08:22:37 GMT, z...@hooked.net (Dale Cook) wrote:

Quote

>My application has a TTreeView control that is central to its function. In that
>control are various items that relate to items in a database. Each of these
>items has at least one piece of data that I would like to store in the
>TreeView with the items name. For example lets say that I want to place a list
>of customers in the TreeView and each customer has a unique customer ID. The
>customer ID is a long integer and I'd like to store that in the Data property
>of the TTreeNode for that customer. However I'm having problems. I thought
>something like this might work:

>procedure TfrmMain.FormCreate(Sender: TObject);
>var
>   iIndex : integer;
>   Company: TTreeNode;
>   iCompanyID: ^LongInt;

>begin

>     Query1.Open;

>     Query1.First;

>     for iIndex := 0 to Query1.RecordCount -1 do
>     begin

>          Company := trvProjects.Items.Add(nil, Query1.Fields[0].AsString);
>          iCompanyID^ := Query1.Fields[1].AsInteger;

Where iCompanyID pointer to ?
May be   Company.Data := Query1.Fields[1].AsInteger;

- Show quoted text -

Quote
>          Company.Data := iCompanyID;

>          Query1.Next;
>     end;

>end;

>Where Query1 is a SQL query that returns the Company name in Field[0] and the
>unique Company ID in Field[1], trvProjects is the TreeView component.

>When I try this piece of code it works for the first iteration and then on the
>second iteration, at the line "Company := trvProjects.Items.Add...", I get an
>EAccessViolation error with the message:
>"Access violation at addess 004165BB. Read of address FFFFFFFF".

>Any help with this would be greatly appreciated.

>TIA
>Dale

Hope this helps.

Re:HELP: Data items in TreeViews


Quote
Dale Cook wrote:

> My application has a TTreeView control that is central to its function. In that
> control are various items that relate to items in a database. Each of these
> items has at least one piece of data that I would like to store in the
> TreeView with the items name. For example lets say that I want to place a list
> of customers in the TreeView and each customer has a unique customer ID. The
> customer ID is a long integer and I'd like to store that in the Data property
> of the TTreeNode for that customer. However I'm having problems. I thought
> something like this might work:

> procedure TfrmMain.FormCreate(Sender: TObject);
> var
>    iIndex : integer;
>    Company: TTreeNode;
>    iCompanyID: ^LongInt;

> begin

>      Query1.Open;

>      Query1.First;

>      for iIndex := 0 to Query1.RecordCount -1 do
>      begin

>           Company := trvProjects.Items.Add(nil, Query1.Fields[0].AsString);
>           iCompanyID^ := Query1.Fields[1].AsInteger;
>           Company.Data := iCompanyID;

>           Query1.Next;
>      end;

> end;

Dale,

I've just implemented a treeview application based on a database which
looks much like windows explorer. The way I would do what you are trying
to do is create a class to contain any company information you need and
instantiate that class for each treenode:

type
  TCustomerRecord = class(TObject)
    CompanyID : LongInt;
  end;

You may want to add other members to this class, and perhaps a
constructor.

Then:
var
  myCustRecord : TCustRecord;
  Company : TTreeNode;
begin

...

  while not Query1.EOF do
    begin
      myCustRecord := TCustRecord.Create;
      myCustRecord.CompanyID := Query1.Fields[1].AsInteger;
      Company := trvProjects.Items.AddObject(nil,
Query1.Fields[0].AsString, myCustRecord);
      Query1.Next;
    end;

Try that.

Steve Elliott

--
********************************************
* To reply remove .NOJUNK from my address *
********************************************

Other Threads