Thank you. The problem is that the text displayed in the node is not the true filename which is stored in the internal FileInfo record of the TShellTreeNode. So, when a node is renamed in the tree, only the Node.Text is changed, but not the filename in the FileInfo.
Unfortunately, the FileInfo is pretty well-sealed within the TShellTreeNode from the outside world, and I am afraid that the issue can only be solved within the ShellTreeViews unit. For test purposes, I added the following public method
function TCustomShellTreeView.Rename(ANode: TTreeNode; ANewName: String): Boolean;
var
oldFullName, newFullName: String;
begin
oldFullName := TShellTreeNode(ANode).FullFilename;
newFullName := TShellTreeNode(ANode).BasePath + ANewName;
if TShellTreeNode(ANode).IsDirectory then
begin
oldFullName := AppendPathDelim(oldFullName);
newFullname := AppendPathDelim(newFullName);
end;
Result := RenameFile(oldFullName, newFullName);
if Result then
begin
TShellTreeNode(ANode).FFileInfo.Name := ANewName;
ANode.Text := ANewName;
end;
end;
And the event handler of your code would be
procedure TForm1.ShellTreeView1Edited(Sender: TObject; Node: TTreeNode;
var S: string);
begin
if not ShellTreeView1.Rename(Node, S) then
begin
S := Node.Text;
MessageDlg('Cannot rename object! Error message:' + sLineBreak + '"' + SysErrorMessage(GetLastOSError) + '"', mtError, [mbOK], 0);
end
end;
This works correctly in my tests. What I don't like is that the user probably expects that the ANewName argument could contain a full path in order to move the file to a different directory. However, if this were allowed the node would have to be moved to a completely different parent node, but it is not guaranteed that the new parent node exists in the tree at all.
Another, more elegant solution would be to override the setter for the node's Text property so that it automatically updates the internal FileInfo field. But this would require to declare the SetText method as virtual. And at the moment I don't overlook the issues that might occur when a new node is getting its caption for the first time. I also could imagine that there are use cases in which the displayed node text should be different from the filename.