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.
This topic is 2 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
JaimeBou
Posts: 27
Last visit: Wed Jan 31, 2024 8:52 am
Has voted: 1 time

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 125 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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 160 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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: Wed Jan 31, 2024 8:52 am
Has voted: 1 time

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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.
This topic is 2 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked