How to use a TTreeView to list all available reports in a program (Views: 100)


I am trying to use the TTreeView to list all the available reports in my application. I would like to click on a specific treenode and then click an button to print a specific report. I don't know what property to use or how can I go about getting the results.


Solve 1:

There are a few ways to do this. The way I usually do this is to allocate memory for the report name and then add this as a pointer to the node. You can then dereference the pointer to get the info back. Here's an example of attaching information from a database.

if wwTable5.RecordCount > 0 then
  TreeNode := Items.AddChildObject(CurrentPatient, 'Treatments', nil);
  TreeNode.ImageIndex := 5;
  TreeNode.SelectedIndex := 5;
  while not wwTable5.EOF do
    NodeRecord^.RecType := Treatment;
    NodeRecord^.RecInfo := wwTable5Number.AsInteger;
    Items.AddChildObject(TeeNode, 'Course: ' + wwTable5CourseNumber.AsString,

I'm allocating a record, filling it with info from the database and then adding the node. You can also cast an integer to a pointer and add it as the object pointer. You can then cast it back to an integer and retrieve the information associated with the node.

Here's an example of getting the info back out of the node:

SelectedNode := SelectedNode.GetFirstChild;
while SelectedNode <> nil do
  if PNodeType(SelectedNode.data)^.RecInfo = FindPatient then
  SelectedNode := SelectedNode.GetNextSibling;

Solve 2:

If you have a two level tree (node -> subnodes(your_reports)). To determine what item is selected use property TTreeView.Selected:

procedure TForm1.PrintButtonClick(Sender: TObject);
  SelectedNode: TTreeNode;
  i: integer;
  SelectedNode := TTreeView1.Selected;
  for i := 0 to SelectedNode.Count - 1
    if SelectedNode.Item[i].Selected then

<< Back to main page