?
Solved

Finding Nodetree when entering the path

Posted on 2003-11-16
6
Medium Priority
?
237 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
  • 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

807 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