Finding Nodetree when entering the path

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
LVL 6
so3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Peter_Commented:
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
kretzschmarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
so3Author Commented:
the code from you  kretzschmar is what i need. thank you

PS: The code from you  Peter_  is not working
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Peter_Commented:
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
kretzschmarCommented:
>Kretzschmar's version looks better :).
thanks :-))

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

meikl ;-)
0
so3Author Commented:
thank you both for your comments

so3
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.