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.
mattys
Posts: 36
Joined: Mon Dec 30, 2019 10:58 am

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: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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: 36
Joined: Mon Dec 30, 2019 10:58 am

Re: TreeView Help

Post by mattys »

This Function Loads the tree in my form
  1. function Load-FolderIntoTree
  2.  
  3. {
  4.  
  5.     param([System.Windows.Forms.TreeView]$TreeView, [string]$Directory)
  6.  
  7.    
  8.  
  9.     #Disable Drawing
  10.  
  11.     $TreeView.BeginUpdate()
  12.  
  13.    
  14.  
  15.     $TreeView.Nodes.Clear()
  16.  
  17.     $folderName = [System.IO.Path]::GetFileName($Directory)
  18.  
  19.    
  20.  
  21.    
  22.  
  23.     $root = $TreeView.Nodes.Add($folderName, $folderName, 1, 1)
  24.  
  25.     $root.ToolTipText = $Directory
  26.  
  27.     $subfolders = [System.IO.Directory]::GetDirectories($Directory)
  28.  
  29.    
  30.  
  31.     if(-not $script:HideSubFolders)
  32.  
  33.     {
  34.  
  35.         foreach($folder in $subfolders)
  36.  
  37.         {
  38.  
  39.             $folderName = [System.IO.Path]::GetFileName($folder)
  40.  
  41.             $node = $root.Nodes.Add($folderName, $folderName, 1, 1)
  42.  
  43.             $node.ToolTipText = $folder
  44.  
  45.         }
  46.  
  47.     }
  48.  
  49.    
  50.  
  51.     $files = [System.IO.Directory]::GetFiles($Directory)
  52.  
  53.     foreach($file in $files)
  54.  
  55.     {
  56.  
  57.         $fileName = [System.IO.Path]::GetFileName($file)
  58.  
  59.         $node = $root.Nodes.Add($fileName, $fileName, 0, 0)
  60.  
  61.         $node.ToolTipText = $file
  62.  
  63.        
  64.  
  65.     }
  66.  
  67.    
  68.  
  69.     $root.Expand()
  70.  
  71.    
  72.  
  73.     #Enable Drawing
  74.  
  75.     $treeview1.EndUpdate()
  76.  
  77. }
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.  
  3. #Event Argument: $_ = [System.Windows.Forms.TreeNodeMouseClickEventArgs]
  4.  
  5.     if($_.Node.ImageIndex -eq 1)
  6.  
  7.     {
  8.  
  9.         #Folder - Open it
  10.  
  11.         Load-FolderIntoTree -TreeView $treeview1 -Directory $_.Node.ToolTipText
  12.  
  13.        
  14.  
  15.         #Open img to picturebox
  16.  
  17.         $picturebox1.image = $_.Node.ToolTipText ##<--This is where I get lost.
  18.  
  19.        
  20.  
  21.     }
  22.  
  23. }
Thank you

jvierra
Posts: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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: 36
Joined: Mon Dec 30, 2019 10:58 am

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: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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: 36
Joined: Mon Dec 30, 2019 10:58 am

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: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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: 36
Joined: Mon Dec 30, 2019 10:58 am

Re: TreeView Help

Post by mattys »

You are right, it is coming up empty.

jvierra
Posts: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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 6 times

Post Reply