Link to home
Start Free TrialLog in
Avatar of hagur
hagur

asked on

Yet another treeview question ....

I've got one treeview which displays a directory and it's subdirectorys and files.

When I double click a node in the treeview, I'd like to get the filename I clicked and the full path.

I almost have this right, all I do is that I take the start path the treeview displays, and then Treeview.Selected.Text, but when there's a parent node in between, the path will not be correct.

So, basically what I need is to find out whether the selected node has a parent, and if it does, I need it's caption.
There fore:

Filename := Startpath+TreeViewParentText+TreeView.Selected.text;

Or something similiar

- Hagur
Avatar of hubdog
hubdog

you want

treeview.selected.parent.text

regards,hubdog
I see your problem. The most easiest thing is to write with every node a class or record containing the entire path to file file/folder and hang it on the node (TTreeNode.Data).

Another possibility is to loop on the treeview as long as the parent/actual node is valid.

I would prefer the first. Depends on you...
Avatar of kretzschmar
hi hagur,

you can do something like this

Function GetFullPath(Node : TTreeNode) : String;
begin
  Result := '';
  While Node <> Nil do
  Begin
    Result := Node.Text + '\'+Result;
    Node := Node.Parent;
  end;
end;


procedure TForm1.TreeView1DblClick(Sender: TObject);
begin
  ShowMessage(GetFullPath(Treeview1.Selected));
end;

meikl
oops,

there will be a backslash to much,

the correction:

Function GetFullString(Node : TTreeNode) : String;
begin
  Result := '';
  While Node <> Nil do
  Begin
    Result := Node.Text + '\'+Result;
    Node := Node.Parent;
  end;
  Delete(Result,Length(result),1);
end;

meikl
Avatar of hagur

ASKER

HubDog,

Although tree.selected.parent.text works, and infact I had already done that, what if the selected node had no parent?  I get an access violation.
So what I really needed was a way to figure out how to determine whether a specified node *had* a parent, and if it did, then do something similar to what you said.

As for kretzschmar, your solution worked
perfectly.

Post your comment as an answer and you'll receive my points.

HubDog, thanks for your input anyway.
hi hagur,

glad to helped you,
good luck again

meikl ;-)
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial