Recent

Author Topic: Getting text of siblings of TreeView parent nodes  (Read 834 times)

maurobio

  • Full Member
  • ***
  • Posts: 102
Getting text of siblings of TreeView parent nodes
« on: December 02, 2019, 10:38:23 pm »
Dear ALL,

I have what looks like a "simple" question, but after some hours googling and browsing the documentation, I could not find any adequate answer for it.

I have a simple TreeView as follows:

Parent 1
(x) Child 1
( ) Child 2
( ) Child 3
   
Parent 2

Parent 3
( ) Child 1
(x) Child 2
   
Parent 4
Child 1

(... and so on...)

Some parent nodes have siblings, other do not. Many of the siblings have checkboxes, but a few do not.

What I want is: (1) for a given Selected node (eg., Parent 1), check if the node has parents *with* checkboxes; (2) if ("and only if", as mathematicians like to say) the Selected node has siblings and they are checkable, get the Text of the checked siblings.

I know the Selected and Text properties of TreeNode, and presume the algorithm should be implemented more or less like this:

Code: Pascal  [Select]
  1. var
  2.         PNode, SNode: TTreeNode;
  3.         AText: string;
  4. begin
  5.         PNode := TTreeView1.Selected; {... get the currently selected node }
  6.         if PNode.HasChildren then {... check if PNode has siblings }
  7.         begin
  8.                 {... check if the siblings have checkboxes }
  9.                 {... if they have chackboxes, get the text of the sibliings }
  10.                 { AText := SNode.Text; }
  11.         end;
  12. end;
  13.  
But I could not proceed beyond that point.

Thanks in advance for any assiatence you can provide.

UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

jamie

  • Hero Member
  • *****
  • Posts: 2168
Re: Getting text of siblings of TreeView parent nodes
« Reply #1 on: December 02, 2019, 11:49:58 pm »
Well, once you get the first child, you can use the GetNextSibling and so on..

as for the Check boxes, those you need to provide images for state from an imagelist….

The StateIndex property determines which image from the list to show on the left side...

A state of 0 could be False, 1 could be true and so on..

Since Boolean states that any number other than 0 is True you can use this as a TRUE state(Checked), if 0 then it's false.

 In the imagelist, have an empty box for index 0 and empty box with a Checkmark in for index 1.

 or use custom images.

 The TtreeNode does not have its own check mark images that I know of in the LCL, altrough I think MS has it in the base control for the branches.


If you need more help just ask.
« Last Edit: December 02, 2019, 11:51:37 pm by jamie »
Number 1 at blue screen app creations!

howardpc

  • Hero Member
  • *****
  • Posts: 3206
Re: Getting text of siblings of TreeView parent nodes
« Reply #2 on: December 03, 2019, 01:26:17 am »
The attached project may do what you are after, though your use of "parents" and "siblings" in your description does not make sense to me.

maurobio

  • Full Member
  • ***
  • Posts: 102
Re: Getting text of siblings of TreeView parent nodes
« Reply #3 on: December 03, 2019, 01:49:12 am »
@jamie,

In fact, I did some progress using GetFirstChild and GetNextChild, although I have not achieved a solution.

@howardpc,

Thanks, I'll take a look at the project you sent.

I meant parent and children, in fact.

Cheers,
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

jamie

  • Hero Member
  • *****
  • Posts: 2168
Re: Getting text of siblings of TreeView parent nodes
« Reply #4 on: December 03, 2019, 02:30:38 am »
I believe if you set the StateIndex of the node to -1 it indicates no image list index for it, which means its not checkable..

  The StateIndex property is the image index into the state image list property..

 Like I said, the TTreeNode does not have a Checked property like that, you need to integrate the StateIndex values as your check points..
 
Any value 0..StateImageList.Count-1 is a value indicator of something.
Number 1 at blue screen app creations!

zeljko

  • Hero Member
  • *****
  • Posts: 1090
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Getting text of siblings of TreeView parent nodes
« Reply #5 on: December 03, 2019, 09:08:07 am »
There's PVirtualNode CheckType, CheckState and States (TVirtualNodeStates).

wp

  • Hero Member
  • *****
  • Posts: 6491
Re: Getting text of siblings of TreeView parent nodes
« Reply #6 on: December 03, 2019, 10:26:37 am »
There's PVirtualNode CheckType, CheckState and States (TVirtualNodeStates).
But this holds for VirtualTreeView only. The thread is about the standard TTreeView.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

zeljko

  • Hero Member
  • *****
  • Posts: 1090
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Getting text of siblings of TreeView parent nodes
« Reply #7 on: December 03, 2019, 10:52:39 am »
pffff sorry for my misreading :)

maurobio

  • Full Member
  • ***
  • Posts: 102
Re: Getting text of siblings of TreeView parent nodes
« Reply #8 on: December 03, 2019, 11:39:46 am »
@zeljko,

VirtualTrees are surely more powerful, but also much more complicated and perhaps beyond my current needs.
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

maurobio

  • Full Member
  • ***
  • Posts: 102
Re: Getting text of siblings of TreeView parent nodes
« Reply #9 on: December 03, 2019, 12:20:23 pm »
Dear ALL,

Just to sum up, using the suggestions by @jamie and @howardpc, I achieved the following solution (shortened for brevity):

Code: Pascal  [Select]
  1. var
  2.   Parent, Child: TTreeNode;
  3. begin
  4.   Parent := TreeView1.Selected;
  5.   if Parent.HasChildren then
  6.   begin
  7.     if Parent.Count > 1 then
  8.     begin
  9.       Child := TreeView1.Selected.GetFirstChild;
  10.       while Child <> nil do
  11.       begin
  12.         {... do something, eg. display Child.Text}
  13.         Child := TreeView1.Selected.GetNextChild(Child);
  14.       end;
  15.     end;
  16.   end;
  17. end;

However, based upon these experiments, I decided to give the problem another tack. I will post any doubts (along with code) in another thread, as they arise.

Cheers,
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

wp

  • Hero Member
  • *****
  • Posts: 6491
Re: Getting text of siblings of TreeView parent nodes
« Reply #10 on: December 03, 2019, 12:26:47 pm »
Code: Pascal  [Select]
  1. var
  2.   Parent, Child: TTreeNode;
  3. begin
  4.   Parent := TreeView1.Selected;
  5.   if Parent.HasChildren then
  6.   begin
  7.     if Parent.Count > 1 then
  8.     begin
  9.       Child := TreeView1.Selected.GetFirstChild;
  10.       while Child <> nil do
  11.       begin
  12.         {... do something, eg. display Child.Text}
  13.         Child := TreeView1.Selected.GetNextChild(Child);
  14.       end;
  15.     end;
  16.   end;
  17. end;

You can simplify by omitting "Parent.HasChildren" and "Parent.Count > 1" because TreeView.Selected.GetFirstChild is nil when there are no children. But make sure that TreeView.Selected is not nil - this might easily happen.

Code: Pascal  [Select]
  1. var
  2.   Child: TTreeNode;
  3. begin
  4.   if TreeView1.Selected = nil then exit;
  5.   Child := TreeView1.Selected.GetFirstChild;
  6.   while Child <> nil do
  7.   begin
  8.     {... do something, eg. display Child.Text}
  9.     Child := TreeView1.Selected.GetNextChild(Child);
  10.   end;
  11. end;
« Last Edit: December 03, 2019, 12:29:06 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

maurobio

  • Full Member
  • ***
  • Posts: 102
Re: Getting text of siblings of TreeView parent nodes
« Reply #11 on: December 03, 2019, 12:36:17 pm »
@wp.

Great, truly great! Thank you!
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home