Board index » delphi » Adding Items to TreeView

Adding Items to TreeView

Hello,

I have a question about adding nodes to a TTreeview component at runtime.
The TreeView component I have all you to set the child levels to CheckBox
or RadioBox.  I have a query that has the following 4 fields:

Role |  Screen |  Object  | Access

The Primary Key is the combination of Role|Screen|Object, so there won't be
any duplicates. I want to add the columns from each row of the query to the
tree view, with the OBJECT column begin Checked\Unchecked based on the value
of the Access column (Y|N).  I'd like it to looks something like this:

*  Role
   | ---  Screen
       | ---  [x] Object

I have set of a while loop that iterates through the table, but I cannot
figure out how to add the nodes without duplicating levels and how to add
the checkbox values.

Thanks for any assistance you can provide

 

Re:Adding Items to TreeView


Quote
In article <8qtdhg$ct...@bornews.borland.com>, Jay Jackson wrote:
> *  Role
>    | ---  Screen
>        | ---  [x] Object

> I have set of a while loop that iterates through the table, but I cannot
> figure out how to add the nodes without duplicating levels and how to add
> the checkbox values.

You need to maintain references to the last node witten at each level and
write new ones as required. You should be able to adapt the rough
approximate code below to something that works for you:

  const ints: array[boolean] of integer = (1,2);
  var roleNode,screenNode,objectNode: TTreeNode;
  var sRole,sScreen,sObject: string;

  roleNode := nil;
  screenNode := nil;
  TreeView1.Items.BeginUpdate;
  try
    while not Query1.EOF do begin
      sRole := Query1.FieldByName('Role').AsString;
      sScreen := Query1.FieldByName('Screen').AsString;
      sObject := Query1.FieldByName('Object').AsString;
      if (roleNode=nil) or (roleNode.Text <> sRole) then
        roleNode := TreeView1.Items.AddChild(nil,sRole);
      if (screenNode=nil) or (screenNode.Text <> sScreen) then
        screenNode := TreeView1.Items.AddChild(roleNode,sScreen);
      end;  
      objectNode := TreeView1.Items.AddChild(screenNode,sObject);
      objectNode.StateIndex :=
ints[Query1.FieldByName('Access').AsBoolean];  // see below
      Query1.Next;  
    end;
  finally
    TreeView1.Items.EndUpdate;
  end;

You will note the reference to StateIndex above. Drop an ImageList on to
the form and set the TreeView1.StateImages property. If you now add images
to the ImageList so that the second and third items show an unchecked box
and a checked box, you get the purpose.

 Mike Orriss (TeamB & Developer Express)
 (Unless stated otherwise, my replies relate to Delphi 5)
 (No unsolicited e-mail replies please)

Other Threads