Jump to content
rudy999

Delphi 11.2 - TTreeview issue not in debug but release mode - Node.HasAsParent(myParentNode) myParentNode

Recommended Posts

I do not use the ttreeview alot. I recently started to use it and while the application was in DEBUG mode there were no issues.

No changes or ifdefs in code but simply changing to release  - and it does not work!

Delphi 11.2 with latest patches.

example code snippet:

calling:

TfrmMain.ttreeViewChange(Sender: TObject; Node: TTreeNode);

//....

          if Node.HasAsParent(myParentNode)  then
          begin
               myParentNode := Node.Parent;
               strParentText := myParentNode.Text;
          end;

when in DEBUG = strParentText string has a value (which I use to compare and branch later in the code)

when in RELEASE = strParentText is empty...

 

Has anyone seen this before?

I am do more tests and my plan is to make a dedicated pre populated treeview to test against. (and send to Embarcadero)

 

Thanks in advance

Edited by Sherlock
Please use source tags

Share this post


Link to post

I guess you means
 

         if Assigned(Node.Parent)  then
          begin
               myParentNode := Node.Parent;
               strParentText := myParentNode.Text;
          end;

 

Share this post


Link to post
8 hours ago, rudy999 said:

TfrmMain.ttreeViewChange(Sender: TObject; Node: TTreeNode);

//....

          if Node.HasAsParent(myParentNode)  then
          begin
               myParentNode := Node.Parent;
               strParentText := myParentNode.Text;
          end;

 

 

The question is, what happens to myParentNode before the if statement. How is it initialized/filled?

Share this post


Link to post

Thank you for your responses, I changed my code to the following and it works:

no need for a 'myParentNode'

 

 if Node.Level > 0 then  begin
          strParentText := Node.Parent.Text;
          end;

 

regards all

Share this post


Link to post
4 hours ago, rudy999 said:

I changed my code to the following and it works:

no need for a 'myParentNode'

Both TTreeNode.Level and TTreeNode.HasAsParent() are recursive, they walk the entire TTreeNode.Parent chain until the root of the tree is reached, or in the case of HasAsParent(), until the specified node is reached.  You don't really need that recursiveness in your situation.  Paul's solution is more efficient and will suit your situation just fine:

if Assigned(Node.Parent) then // or: if Node.Parent <> nil then
begin
  strParentText := Node.Parent.Text;
end;

Alternatively, to eliminate a redundant function call:

var myParentNode: TTreeNode := Node.Parent;
if Assigned(myParentNode) then // or: if myParentNode <> nil then
begin
  strParentText := myParentNode.Text;
end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×