Mirror

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


Problem/Question/Abstract:

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.

if wwTable5.RecordCount > 0 then
begin
  TreeNode := Items.AddChildObject(CurrentPatient, 'Treatments', nil);
  TreeNode.ImageIndex := 5;
  TreeNode.SelectedIndex := 5;
  wwTable5.First;
  while not wwTable5.EOF do
  begin
    New(NodeRecord);
    NodeRecord^.RecType := Treatment;
    NodeRecord^.RecInfo := wwTable5Number.AsInteger;
    Items.AddChildObject(TeeNode, 'Course: ' + wwTable5CourseNumber.AsString,
      NodeRecord);
    wwTable5.Next;
  end;
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:

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


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);
var
  SelectedNode: TTreeNode;
  i: integer;
begin
  SelectedNode := TTreeView1.Selected;
  for i := 0 to SelectedNode.Count - 1
  begin
    if SelectedNode.Item[i].Selected then
      MyPrintReport_procedure(Item[i].Text);
  end;
end;

<< Back to main page