2009. június 19., péntek

How to add items of a TListBox as sub-items to a selected tree node


Problem/Question/Abstract:

I have TreeView1, Button1 and ListBox1. ListBox one has x number of items. I need to be able to click Button1 and the items in ListBox1 are inserted as sub-items to the selected tree-node.

Answer:

var
  ix: integer;
  parentnode: TTreeNode;

  TreeView.Items.BeginUpdate;
try
  parentnode := TreeView.FocusedNode;
  for ix := 0 to ListBox1.Items.Count - 1 do
  begin
    if parentnode = nil then
      Tree.Items.Add(nil, ListBox1[ix])
    else
      Tree.Items.AddChild(parentnode, ListBox1[ix]);
  end;
finally
  TreeView.Items.EndUpdate;
end;

Nincsenek megjegyzések:

Megjegyzés küldése