views:

868

answers:

2

hi all

can anyone tell me how to

  • create nodes
  • enable/disable a node

I want to know how to add nodes at application run-time (in the main form's OnCreate event)

A: 

Adding nodes:

function FindRootNode(ACaption: String; ATreeView: TTreeView): TTreeNode; 
var LCount: Integer; 
begin 
  result := nil; 
  LCount := 0; 
  while (LCount < ATreeView.Items.Count) and (result = nil) do 
  begin 
    if (ATreeView.Items.Item[LCount].Text = ACaption) and (ATreeView.Items.Item[LCount].Parent = nil) then 
      result := ATreeView.Items.Item[LCount]; 
    inc(LCount); 
  end; 
end;

...

var LDestNode: TTreeNode; 
begin 
  LDestNode := FindRootNode('category', TreeView1); 
  if LDestNode <> nil then 
  begin 
    TreeView1.Items.AddChild(LDestNode, 'node1'); 
    TreeView1.Items.AddChild(LDestNode, 'node2'); 
  end; 
end;

(see also http://msdn.microsoft.com/en-us/library/70w4awc4.aspx)

Disabeling a node

As far as I know, there is no way to disable a TreeNode. Only thing you could do is intercept the beforeSelect-event and cancel the selection there. Not so nice.

Mef
There is a "Selectable" property that exists in TCustomViewItem, which the Item property descends from. That might behave as a kind of disabling mechanism.
Mark Wilkins
i know how selectable works, but I want to do this in MainForm.OnCreate event
Remus Rigo
@Mark: In what version of Delphi is this "TCustomViewItem"? I cannot find it. @Remus: Does that mean you're trying to avoid event handlers. Otherwise you could populate a "disabled nodes" list or array in form's OnCreate and check against these items on OnChanging and OnCustomDrawItem of the TreeView.OTOH there's a TVIS_EX_DISABLED flag for Shell Controls > ver. 6.00 (Vista?), but all it really does is to draw the text with the disabled color and not to draw the selected look if you click on it. The item, in fact, is selected.
Sertac Akyuz
@Sertac: I was looking at the Delphi 7 help file.
Mark Wilkins
Thanks Mark. It doesn't exist in D2007. Weird..
Sertac Akyuz
+3  A: 

@Remus, here you have an simple example adding nodes.

Adding a root Node (level 0)

Var
  Node : TTreeNode;
begin
   //function TTreeNodes.Add(Sibling: TTreeNode; const S: string): TTreeNode;
   Node:=TreeView1.Items.Add(nil,'My Root Node') ;
   Node.ImageIndex:=0;//now you can change any  property of the node
end;

Adding a child Node (level > 0)

//in this case we add a child node in the current selected node.
    Var
      Node : TTreeNode;
    begin       
       if TreeView1.Selected= nil then exit;       
       Node:=TreeView1.Items.AddChild(TreeView1.Selected,'My Child Node') ;
       Node.ImageIndex:=0;//now you can change any property of the node
    end;

Adding many nodes

if you wanna add many nodes using a loop or something else you must use BeginUpdate before making the changes to the treeview. When all changes are complete, call EndUpdate to show the changes on screen. BeginUpdate and EndUpdate prevent excessive redraws and speed up processing time when nodes are added, deleted, or inserted.

Var
  Node : TTreeNode;
  i    : Integer;
begin
  TreeView1.Items.BeginUpdate;
  try
   for i:=1 to 100 do
   begin
    Node:=TreeView1.Items.Add(nil,'My Root Node '+IntToStr(i)) ;
    Node.ImageIndex:=0;
   end;
  finally
  TreeView1.Items.EndUpdate;
  end;
end;

About disable a node, does not exist any property like that.

RRUZ