Solved

Finding Nodetree when entering the path

Posted on 2003-11-16
6
231 Views
Last Modified: 2010-04-05
I have a treeview with items like:
Database
      name1
      name2
      name3
            name1
            name2
            name3
                  xxxx
something
      x
      y      
            z

..........


I have these items stored into an array like:
Database\Name1
Database\name2
..
Database\name3\name1  
....

I want to make a function returning the Node of the tree when entering the path:
For example for path: Database\name3\name3\xxxx to return the nodetree.

Do you have any ideas on how to do this function?


so3
0
Comment
Question by:so3
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Expert Comment

by:Peter_
ID: 9760563
I've run some tests and I think this works if Edit1.Text is assigned a value such as 'Database\Name1\Name1'.

Though it might need some further errorhandling perhaps, in case 'path' does not exist.

-----------------------------------------------------------------------------

function GetNode(TreeView: TTreeView; Path: String): TTreeNode;
var
 CurrentNode: TTreeNode;

  function GetChild(NodeName: String): TTreeNode;
  var
   CurrentChild: TTreeNode;
  begin
   if (CurrentNode=nil) then
   begin
    Result := nil;
    Exit;
   end;
   CurrentChild := CurrentNode.GetFirstChild;
   if (CurrentChild=nil) then
   begin
    Result := nil;
    Exit;
   end;
   if (CurrentChild.Text = NodeName) then
    Result := CurrentChild
   else
   begin
    repeat
     CurrentChild := CurrentNode.GetNextChild(CurrentChild);
    until ( (CurrentChild = nil) or (CurrentChild.Text = NodeName)  );
    Result := CurrentChild;
   end;
  end;

  function CutPath: String;
  begin
   Path := Copy(Path, Pos('\',Path)+1, Length(Path));
   Result := Path;
  end;

  function GetChildName: String;
  var
   I: Integer;
  begin
   I := Pos('\',Path);
   if (I<>0) then
    Result := Copy(Path, 1, Pos('\',Path)-1)
   else
   begin
    Result := Path;
    Path := '';
   end;
  end;

begin
   CurrentNode := TreeView.Items.GetFirstNode;
   while (CutPath<>'') do
   begin
    CurrentNode := GetChild(GetChildName);
   end;
   Result := CurrentNode;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 Node: TTreeNode;
begin
 Node := GetNode(TreeView1,Edit1.Text);
 if (node<>nil) then
  showmessage(Node.Text);
end;
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 9762098
well,

my version

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function splitstr(var astring : String; Delimiter : String) : String;
var
  p : Integer;
begin
  result := '';
  if AString <> '' then
  begin
    p := pos(Delimiter,AString);
    if p > 0 then
    begin
      result := copy(AString,1,p-1);
      AString := copy(AString,p+length(Delimiter),maxLongInt);
    end
    else
    begin
      result := AString;
      AString := '';
    end;
  end;
end;

function FindNodeByCaption(ATreeView : TTreeView; ParentNode : TTreeNode; ACaption : String) : TTreeNode;
begin
  result := Nil;
  if ParentNode = Nil then
    result := ATreeView.TopItem
  else
    Result := ParentNode.getFirstChild;
  while assigned(Result) and (Result.Text <> ACaption) do
    Result := Result.getNextSibling;
end;

function FindNode(ATree : TTreeView; APath : String) : TTreeNode;
var
  s, se : String;
  found : Boolean;
begin
  s := APath;
  found := true;
  result := nil;
  while (s <> '') and (found) do
  begin
    se := splitstr(s,'\');
    result := FindNodeByCaption(ATree,Result,se);
    found := assigned(result);
  end;
  if not found then
    result := nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
var ANode : TTreeNode;
begin
  ANode := FindNode(TreeView1,edit1.Text);
  if assigned(ANode) then
    ANode.MakeVisible //or maybe set the focus
  else
    showmessage('Node not found');
end;

end.

meikl ;-)
0
 
LVL 6

Author Comment

by:so3
ID: 9767690
the code from you  kretzschmar is what i need. thank you

PS: The code from you  Peter_  is not working
0
Independent Software Vendors: 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!

 
LVL 3

Expert Comment

by:Peter_
ID: 9770419
Well, as far as I can see both codes does pretty much the same thing if you break it down to parts :).

Maybe I missed some small thing then? These recurtions tend to sometimes make one dizzy :). It worked fine on my tree though. Kretzschmar's version looks better :).
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9770430
>Kretzschmar's version looks better :).
thanks :-))

did not test your code, peter,
i guessed you did it already

meikl ;-)
0
 
LVL 6

Author Comment

by:so3
ID: 9774833
thank you both for your comments

so3
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
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.
Suggested Courses
Course of the Month4 days, 21 hours left to enroll

636 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