Recent

Author Topic: [SOLVED] child of parent node  (Read 1042 times)

petevick

  • Sr. Member
  • ****
  • Posts: 419
[SOLVED] child of parent node
« on: February 02, 2024, 05:05:48 pm »
I have the following code, it displays a message based on the contents of a selected folder in a TShellTreeView. On the first selction I get no error but I get an "External: SIGSEGV" error on the second selection at line 9, as pChild is Nil at line 8. I know for sure that the selected folder does have a child as line 7 qualifies it, also cParent is valid just before the error.
So what have I done that causes the error ?

Code: Pascal  [Select][+][-]
  1. procedure TBrowser.SelectFolderClick(Sender: TObject);
  2. var
  3.   Reply: Integer = mrYes;
  4.   pChild, cParent: TTreeNode;
  5. begin
  6.  cParent:= myTreeView.Selected;
  7.    if cParent.HasChildren = True then begin
  8.      pChild:= cParent.GetFirstChild;
  9.      if (pChild.Text = 'icons') and (pChild.GetNextChild(pChild)=Nil) then
  10.        Reply:= MessageDlg('Existing Folder',
  11.            'You have selected an icons folder'
  12.            , mtConfirmation, [mbClose], 0)
  13.      else
  14.        Reply:= MessageDlg('Unsuitable Folder',
  15.            'The selected folder is not empty.'
  16.            , mtWarning, [mbClose], 0);
  17.    end;
  18.   if Reply = mrYes then ModalResult:= mrYes;
  19. end;
  20.  
« Last Edit: February 03, 2024, 12:42:59 pm by petevick »
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

bytebites

  • Hero Member
  • *****
  • Posts: 704
Re: child of parent node
« Reply #1 on: February 02, 2024, 05:31:11 pm »
Maybe cParent instead if pChild?
Code: Pascal  [Select][+][-]
  1.  if (pChild.Text = 'icons') and (cParent.GetNextChild(pChild)=Nil) then

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: child of parent node
« Reply #2 on: February 02, 2024, 05:34:55 pm »
Maybe cParent instead if pChild?
Code: Pascal  [Select][+][-]
  1.  if (pChild.Text = 'icons') and (cParent.GetNextChild(pChild)=Nil) then
Possibly, but it would still fail at line 9 as pChild is Nil on the second selection.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: child of parent node
« Reply #3 on: February 02, 2024, 05:38:57 pm »
Even using the following I still get the same error on the second selection....

Code: Pascal  [Select][+][-]
  1. procedure TBrowser.SelectFolderClick(Sender: TObject);
  2. var
  3.   Reply: Integer = mrYes;
  4.   cCount: Integer;
  5.   pChild, cParent: TTreeNode;
  6. begin
  7.  cParent:=myTreeView.Selected;
  8.    if cParent.HasChildren then begin
  9.      cCount:= cParent.SubTreeCount;
  10.      pChild:= cParent.GetFirstChild;
  11.      if (pChild.Text = 'icons') and (cCount = 1) then
  12.        Reply:= MessageDlg('Existing Folder',
  13.            'You have selected an icons folder'
  14.            , mtConfirmation, [mbClose], 0);
  15.      if (pChild.Text <> 'icons') and (cCount > 1) then
  16.        Reply:= MessageDlg('Unsuitable Folder',
  17.            'The selected folder is not empty.'
  18.            , mtWarning, [mbClose], 0);
  19.    end;
  20.    if Reply = mrYes then ModalResult:= mrYes;
  21. end;
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

ASerge

  • Hero Member
  • *****
  • Posts: 2389
Re: child of parent node
« Reply #4 on: February 02, 2024, 06:10:29 pm »
In some cases, HasChildren doesn't mean that it has items, but that's just that it's a folder. So you can do it like this:
Code: Pascal  [Select][+][-]
  1. procedure TBrowser.SelectFolderClick(Sender: TObject);
  2. const
  3.   CCaption: array[Boolean] of string = ('Unsuitable Folder', 'Existing Folder');
  4.   CMsg: array[Boolean] of string = ('The selected folder is not empty.', 'You have selected an icons folder');
  5.   CDlgType: array[Boolean] of TMsgDlgType = (mtWarning, mtConfirmation);
  6. var
  7.   Suitable: Boolean;
  8.   Child, Selected: TTreeNode;
  9. begin
  10.   Selected := myTreeView.Selected;
  11.   if Selected = nil then
  12.     Exit;
  13.   Child := Selected.GetFirstChild;
  14.   if Child <> nil then
  15.   begin
  16.     Suitable := SameFileName(Child.Text, 'icons') and (Child.GetFirstChild = nil);
  17.     if MessageDlg(CCaption[Suitable], CMsg[Suitable], CDlgType[Suitable], [mbClose], 0) = mrYes then
  18.       ModalResult := mrYes;
  19.   end;
  20. end;

wp

  • Hero Member
  • *****
  • Posts: 12676
Re: child of parent node
« Reply #5 on: February 02, 2024, 06:10:35 pm »
It could be that child node has not yet been created even although the parent's HasChildren signals the presence of children. This is just a flag to indicate that the expand icon must be drawn, but the populating process might have stopped at the parent level. Partial creation of the nodes has been implemented to save time and memory because directory trees can be huge.

It is my impression that thinking in terms of the file system is a better strategy for special navigating the shelltree than thinking in terms of nodes.
« Last Edit: February 02, 2024, 08:27:19 pm by wp »

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: child of parent node
« Reply #6 on: February 02, 2024, 06:14:00 pm »
It could be that child node has not yet been created even although the parent's HasChildren signals the presence of children. This is just a flag to indicate that the expand icon must be drawn, but the populating process might have stopped at the parent level. Partial reading of the nodes has been implemented to save time and memory because directory trees can be huge.

It is my impression that thinking in terms of the file system is a better strategy for special navigating the shelltree than thinking in terms of nodes.
That makes sense wp. I think I'll have to be more generic with the mesages.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: child of parent node
« Reply #7 on: February 03, 2024, 12:42:37 pm »
Using myTreeView.Path and DirectoryExists() I can get the same message results.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

 

TinyPortal © 2005-2018