?
Solved

Basic Treeview Help

Posted on 2003-02-21
3
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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 my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

770 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