Board index » delphi » Adding TreeView Nodes from a ListView SubItem

Adding TreeView Nodes from a ListView SubItem

I'm not sure how to add several nodes to a TreeView from a ListView's
SubItems

My TreeView looks something like this:

Root(0)
   Node(1)
      Node(2)
      Node(3)
      Node(4)
      Node(5)
      Node(6)
   Node(7)

I need to add all the strings from a ListView's SubItem[3] under Node(2) -
SubItem[4] under Node(3), etc up to Node(6).  How can I do this when when I
don't know what the Node's AbsoluteIndex will be a a given time as the
ListView's SubItems may change when new information is added.

Thanx
  Gus

 

Re:Adding TreeView Nodes from a ListView SubItem


Quote
In article <393371f8@dnews>, Gus wrote:
> I need to add all the strings from a ListView's SubItem[3] under Node(2) -
> SubItem[4] under Node(3), etc up to Node(6).

How do you know that a specific ListItem needs to refer to Node(2) thru
Node(6) in the first place?
How are your nodes created (do you make use of the ListView contents?)

In this type of scenario, I find it is easier to maintain a central object
(TStringList, TList, TCollection or whatever) and populate both TreeView and
ListView from it.

If you give us a few more details, I'm sure that we can help.

 Mike Orriss (TeamB)
 (Unless stated otherwise, my replies relate to Delphi 4.03/5.00)
 (Unsolicited e-mail replies will most likely be ignored)

Re:Adding TreeView Nodes from a ListView SubItem


The nodes are created at design time and the ListView is populated on start
up.  The seven main nodes never change.  The ListView hodes information
regarding our client's status

ApplicationNode
   ClientsNode
      ListByName
         All client names should be added here from ListView's SubItem[0]
      ListByTown
         Town names should be added here from ListView's SubItem[1] - there
can be no duplicates here
      ListByCity
         City names should be added here from ListView's SubItem[2] - there
can be no duplicates here
      ListByPostalCode
         Postal codes should be added here from ListView's SubItem[3] -
there can be no duplicates here
      ListByEMailAddress
         Email addresses should be added here from ListView's SubItem[4]
   ApplicationNode

Hope this helps Mike

Regards
  Gus

"Mike Orriss (TeamB)" <m...@3kcc.co.uk> wrote in message
news:VA.0000198a.0f53c1e8@mikemain...

Quote
> In article <393371f8@dnews>, Gus wrote:
> > I need to add all the strings from a ListView's SubItem[3] under
Node(2) -
> > SubItem[4] under Node(3), etc up to Node(6).

> How do you know that a specific ListItem needs to refer to Node(2) thru
> Node(6) in the first place?

> How are your nodes created (do you make use of the ListView contents?)

> In this type of scenario, I find it is easier to maintain a central object
> (TStringList, TList, TCollection or whatever) and populate both TreeView
and
> ListView from it.

> If you give us a few more details, I'm sure that we can help.

>  Mike Orriss (TeamB)
>  (Unless stated otherwise, my replies relate to Delphi 4.03/5.00)
>  (Unsolicited e-mail replies will most likely be ignored)

Re:Adding TreeView Nodes from a ListView SubItem


Quote
In article <39338bb3@dnews>, Gus wrote:
> Hope this helps

Well sort of. I have a bit of difficulty in as much as your design doesn't
actually make any sense to me.

However, I think that your main difficulty is in maintaining usable
references to your main nodes. The way around it is to declare an array of
TTreeNodes and save your main node references before you add more nodes:

  var MainNodes: array of TTreeNode;
  var ix: integer;

  SetLength(MainNodes,TreeView.Items.Count);
  for ix := 0 to TreeView.Items.Count-1 do
    MainNodes[ix] := TreeView.Items[ix]

You can then use MainNodes[1], say, as the parent node of added items.

I assume that you want to iterate through the ListView rows adding
TreeNodes as appropriate?

  var item: TListItem;
  var ix,ix2: integer;

  for ix := 0 to ListView.Items.Count -1 do begin
    item := ListView.Items[ix];
    TreeView.Items.AddChild(MainNodes[1],item.Caption);
    for ix2 := 0 to 4 do
      TreeView.Items.AddChild(MainNodes[ix2+2],item.SubItems[ix2]);
  end;

The code above does not check for duplicates, but this can easily be added
by changing the last line to a funcion call, e.g

   for ix2 := 0 to 4 do
     AddChildNode(MainNodes[ix2+2],item.SubItems[ix2]);

where AddChildNode might look like:

  procedure TForm1.AddChildNode(node: TTreeNode; const text: string);
  var ix: integer;
  begin
    for ix := 0 to node.Count-1 do begin
      if node.Item[ix].Caption = text then exit;  // if duplicate
    end;
    TreeView.Items.AddChild(node,text);
  end;

(untested)

 Mike Orriss (TeamB)
 (Unless stated otherwise, my replies relate to Delphi 4.03/5.00)
 (Unsolicited e-mail replies will most likely be ignored)

Other Threads