Adding child nodes to treeview programmatically

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
JaimeBou
Posts: 27
Last visit: Mon Apr 19, 2021 10:20 am

Adding child nodes to treeview programmatically

Post by JaimeBou »

I am trying to programmatically add child nodes under a root treeview control.

The nodes do appear, but not as child nodes.

I have looked at some of the demo files provided in this forum and just can't seem to figure out what I am missing.

I could not figure out how to use the select code feature so I added the sample code as an attachment.

Can you help me please.
Attachments
treeviewcode.txt
sample code
(376 Bytes) Downloaded 11 times

jvierra
Posts: 14787
Last visit: Wed May 12, 2021 7:10 am
Answers: 9
Has voted: 3 times
Been upvoted: 9 times

Re: Adding child nodes to treeview programmatically

Post by jvierra »

Each "node" has a "Nodes" property which is where to place children of a node.

Here is one good example:
Attachments
Demo-TreeviewFolders.psf
(31.52 KiB) Downloaded 8 times

jvierra
Posts: 14787
Last visit: Wed May 12, 2021 7:10 am
Answers: 9
Has voted: 3 times
Been upvoted: 9 times

Re: Adding child nodes to treeview programmatically

Post by jvierra »

The following tutorial will help you to learn how to use the TreeView control which is one of the most complex controls in the toolbox and is always a challenge for anyone who has limited or no experience with WinForms. Take time and try all of the example code until you understand what the control does and how MS designed it to be used.

The following is the MSDN documents and description: https://docs.microsoft.com/en-us/dotnet ... mework-4.8
This is the Sapien PowerShell version: https://info.sapien.com/index.php/guis/ ... ew-control

User avatar
JaimeBou
Posts: 27
Last visit: Mon Apr 19, 2021 10:20 am

Re: Adding child nodes to treeview programmatically

Post by JaimeBou »

Thanks for your response.

I actually did study this control and viewed your demo file and the sapien treeview control documentation prior to posting.

The link that you provided and the additional information regarding each node having its own node was the key (treeView1.Nodes[0].Nodes.Add("Child 1");).

I may have missed it, but I didn't see this information in the sapien treeview control document.

I modified my code to: $newNode = $WS_TV_SRM_BCAWEB.Nodes[0].Nodes.Add($DataElement.ws) and that solved it.

Thank you.

jvierra
Posts: 14787
Last visit: Wed May 12, 2021 7:10 am
Answers: 9
Has voted: 3 times
Been upvoted: 9 times

Re: Adding child nodes to treeview programmatically

Post by jvierra »

I know it is not explicit but "tree" means nodes within nodes. It is an old structural reference meaning that things are arranged in a hierarchy that branches. Also the MSDN documentation is always where to go for detailed information about all controls as there are all properties and methods listed with examples.

Glad you got it working. The TV is one of the more challenging controls even for experienced programmers so take your time.

Post Reply