TreeView Help

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 3 years and 3 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
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

TreeView Help

Post by mattys »

I have statically created a TreeView with the TreeNode Editor.

Is it possible that when a node is selected to display an image within a picturebox?

Im having difficulty with this relationship.

Thanks in Advance
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TreeView Help

Post by jvierra »

It is not possible to understand what you are trying to ask. I recommend starting by reviewing the articles in this link to gain some understanding of how to work with forms and controls then try and ask your question again.

https://info.sapien.com/index.php/guis/ ... nistrators
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: TreeView Help

Post by mattys »

This Function Loads the tree in my form
  1. function Load-FolderIntoTree
  2. {
  3.     param([System.Windows.Forms.TreeView]$TreeView, [string]$Directory)
  4.    
  5.     #Disable Drawing
  6.     $TreeView.BeginUpdate()
  7.    
  8.     $TreeView.Nodes.Clear()
  9.     $folderName = [System.IO.Path]::GetFileName($Directory)
  10.    
  11.    
  12.     $root = $TreeView.Nodes.Add($folderName, $folderName, 1, 1)
  13.     $root.ToolTipText = $Directory
  14.     $subfolders = [System.IO.Directory]::GetDirectories($Directory)
  15.    
  16.     if(-not $script:HideSubFolders)
  17.     {
  18.         foreach($folder in $subfolders)
  19.         {
  20.             $folderName = [System.IO.Path]::GetFileName($folder)
  21.             $node = $root.Nodes.Add($folderName, $folderName, 1, 1)
  22.             $node.ToolTipText = $folder
  23.         }
  24.     }
  25.    
  26.     $files = [System.IO.Directory]::GetFiles($Directory)
  27.     foreach($file in $files)
  28.     {
  29.         $fileName = [System.IO.Path]::GetFileName($file)
  30.         $node = $root.Nodes.Add($fileName, $fileName, 0, 0)
  31.         $node.ToolTipText = $file
  32.        
  33.     }
  34.    
  35.     $root.Expand()
  36.    
  37.     #Enable Drawing
  38.     $treeview1.EndUpdate()
  39. }
This event opens a folder in tree. The files in the folder(s) have .png image. When a user selects a file(img) in folder, I'd like the picturebox to display that image from that path($_.Node.ToolTipText)..
  1. $treeview1_NodeMouseDoubleClick=[System.Windows.Forms.TreeNodeMouseClickEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.TreeNodeMouseClickEventArgs]
  3.     if($_.Node.ImageIndex -eq 1)
  4.     {
  5.         #Folder - Open it
  6.         Load-FolderIntoTree -TreeView $treeview1 -Directory $_.Node.ToolTipText
  7.        
  8.         #Open img to picturebox
  9.         $picturebox1.image = $_.Node.ToolTipText ##<--This is where I get lost.
  10.        
  11.     }
  12. }
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: TreeView Help

Post by jvierra »

I still cannot understand what you are asking. Tree nodes have an image property. Please look at the documentation to see how to use the image property and how to display and hide images. You cannot just add a PictureBox to a tree control.

If you are asking how to load an image file into a PictureBox then look at the control reference and it will show you how to do this.
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: TreeView Help

Post by mattys »

O You mean like this, right?
$picturebox1.image = [System.Drawing.Image]::FromFile($treeview.selectednode.fullpath)

but I get this error- Exception calling "FromFile" with "1" argument(s): "The path is not of a legal form."
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TreeView Help

Post by jvierra »

Use the debugger to see what you have in the variable. "Fullpath" on a node is the nodes path and not the data path. Where did you store the file path?
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: TreeView Help

Post by mattys »

The node path and path of desired image are the same.

Thanks for help, I will keep working on this task.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TreeView Help

Post by jvierra »

Not according to the error. Did you look at it in the debugger? You can just use a Write-Host to output the value to see if it is correct.
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: TreeView Help

Post by mattys »

You are right, it is coming up empty.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TreeView Help

Post by jvierra »

Here is an old sampler I built a few years ago. It will help you understand how to navigate and reference nodes in a tree.
Attachments
Demo-TreeviewFolders.psf
(31.47 KiB) Downloaded 91 times
This topic is 3 years and 3 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