Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Basic Treeview Help

Posted on 2003-02-21
3
Medium Priority
?
272 Views
Last Modified: 2010-04-04
As a newbie in treeview i dont understand how to use it!
I have a database with name4s and places.

I would like to have these presented in a treeview like

City 1
--- Smith
--- Wilson
--- Bush
City 2
--- Burke
--- Lopez
--- Johnson

And when clicking on a child-nod i want to retrieve the name of node and the parent nod.
Can someone give me an easy answer how to do it?
(Using Delphi 7 and component ElTree)
0
Comment
Question by:SvanteMusic
3 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7992479
well don't know about ElTree,
but usual it works like

in the onchange-event of the tree use

if node.level = 2 then  //only on a child
  showmessage('Child:'+Node.text +#10+'Parent:'+Node.Parent.text);

just from head

meikl ;-)
0
 
LVL 2

Accepted Solution

by:
DeNavigator earned 600 total points
ID: 7992525
This is what I use with a standard TreeView. I don't know about ElTree. But I presume you can do something similar.

procedure TForm1.ReadData;
var
  aParent: String;
  mIndex: Integer;
begin
  with TreeView1 do begin
    Items.Clear;
    Items.BeginUpdate;
    with DataModule1 do begin
      with TableLocaties do begin
        First;
        while not Eof do begin
          AddMyNode(0, '', FieldByName('Schoolnaam').AsString);
          Next;
        end;
      end; // with TableLocaties
      // Lokalen opnemen in de Treeview;
      with TableLokalen do begin
        First;
        while not Eof do begin
          if TableLocaties.Locate('ID_Nummer', FieldByName('Gebouw').AsString, []) then begin
            aParent := TableLocaties.FieldByName('Schoolnaam').AsString;
            AddMyNode(1, aParent, FieldByName('Lokaalnaam').AsString);
          end else begin
            ShowMessage('Cannot find parent!');
          end;
          Next;
        end;
      end; // with TableLokalen
    end; // with DataModule1
    Items.EndUpdate;
  end; // with TreeView1
end;

procedure TFormHoofd.AddMyNode(mIndex: Integer; aParent, Naam: String);
var
  WerkNode : TTreeNode;
begin
  WerkNode := nil;
  case mIndex of
    0 : begin
      WerkNode := TreeView1.Items.Add(nil, Naam);
      WerkNode.Data := DataModule1.TableLocaties.GetBookmark;
    end; // Index = 0, dus een locatie
    1 : begin
      WerkNode := TreeView1.Items.AddChild(FindNode(aParent), Naam);
      WerkNode.Data := DataModule1.TableLokalen.GetBookmark;
    end; // Index = 1, dus een lokaal
  end; // case
  if Assigned(Werknode) then begin
    with WerkNode do begin
      DropTarget := True;
      ImageIndex := mIndex;
      SelectedIndex := mIndex;
    end;
    WerkNode.MoveTo(WerkNode.Parent, naAddChild);
  end;
end;

function TFormHoofd.FindNode(Naam: String): TTreeNode;
var
  Teller : integer;
begin
  result := nil;
  for Teller := 0 to (TreeView1.Items.Count - 1) do begin
    if TreeView1.Items[Teller].Text = Naam then begin
      Result := TreeView1.Items[Teller];
      Break;
    end;
  end;
end;

0
 

Author Comment

by:SvanteMusic
ID: 8000564
Thanks , your code helped me.

It get me moving on, used only a fragment of it. But introduced me in treeview :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question