Author: Tomas Rutkauskas
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.
Answer:
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.
1 if wwTable5.RecordCount > 0 then2 begin3 TreeNode := Items.AddChildObject(CurrentPatient, 'Treatments', nil);
4 TreeNode.ImageIndex := 5;
5 TreeNode.SelectedIndex := 5;
6 wwTable5.First;
7 whilenot wwTable5.EOF do8 begin9 New(NodeRecord);
10 NodeRecord^.RecType := Treatment;
11 NodeRecord^.RecInfo := wwTable5Number.AsInteger;
12 Items.AddChildObject(TeeNode, 'Course: ' + wwTable5CourseNumber.AsString,
13 NodeRecord);
14 wwTable5.Next;
15 end;
16 end;
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:
17 SelectedNode := SelectedNode.GetFirstChild;
18 while SelectedNode <> nildo19 begin20 if PNodeType(SelectedNode.data)^.RecInfo = FindPatient then21 break;
22 SelectedNode := SelectedNode.GetNextSibling;
23 end;
Solve 2:
If you have a two level tree (node -> subnodes(your_reports)). To determine what
item is selected use property TTreeView.Selected:
24 procedure TForm1.PrintButtonClick(Sender: TObject);
25 var26 SelectedNode: TTreeNode;
27 i: integer;
28 begin29 SelectedNode := TTreeView1.Selected;
30 for i := 0 to SelectedNode.Count - 1
31 begin32 if SelectedNode.Item[i].Selected then33 MyPrintReport_procedure(Item[i].Text);
34 end;
35 end;