TreeView Node Selection

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
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.
User avatar
JaimeBou
Posts: 12
Joined: Fri Feb 09, 2018 9:57 am

TreeView Node Selection

Post by JaimeBou » Thu Jun 20, 2019 4:57 am

I am new to the Treeview and for the most part, I have it working well. I am using checkboxes in my situation and wanted to know if there is a way to make the node text NOT selectable?

I want to force the user to only have the checkboxes as an option.

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: TreeView Node Selection

Post by jvierra » Thu Jun 20, 2019 7:22 am

There is really no way to do what you ask as asked but the following example shows how to manage node selection and checking of the boxes.
Attachments
Demo-TreeviewSelecting.psf
(29.78 KiB) Downloaded 17 times

User avatar
JaimeBou
Posts: 12
Joined: Fri Feb 09, 2018 9:57 am

Re: TreeView Node Selection

Post by JaimeBou » Thu Jun 20, 2019 11:10 am

Thank you. I will review the demo file.

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: TreeView Node Selection

Post by jvierra » Thu Jun 20, 2019 11:56 am

Just a little background. Of all controls in WinForms "Item Selected" is managed by the OS. It does this by sending messages to the control on mouse events and on keyboard events. The highlighting is managed by the system but, with many con trolls, can be customized. This is true for ListView and DataGridView. TreeView is an old sample control that evolved to being useful and MS tucked into the controls package.

In Visual Studio we would create an inherited class and customize this. This is much harder in PowerShell. What most programmers do is to use a third party control that is more specifically designed to match the requirement. It is possible to do this in PowerShell but not that easy in PSS. In PSS you would have to swap the control after it has been created and then copy the properties from the existing TreeView. You will also not get any design time help. One day the great programmers at Sapien might find time to allow us to add custom controls to the toolbox. It can be done but it is not easy since most vendor controls target specific languages and not PowerShell. Net controls do make this much easier but there are disconnects between many controls and the basic nature of PS like threading.

One thing that I have never tried in PS ism to use the item "paint" event to override the system highlighting. I am not sure if it can be done with the tree view. To prevent the mouse form selecting can be done by tracking the mouse and excluding mouse events when over the controls items label. This would require fully generating the item in graphics; not an easy thing to do if you are not a programmer but it can be done.

User avatar
JaimeBou
Posts: 12
Joined: Fri Feb 09, 2018 9:57 am

Re: TreeView Node Selection

Post by JaimeBou » Thu Jun 20, 2019 1:11 pm

I took a look at your demo file and noticed something that would be helpful to me. When the node text is selected, the checkbox is ALSO selected. My code does not do that. I could not confirm what code in the demo provides this functionality?

User avatar
JaimeBou
Posts: 12
Joined: Fri Feb 09, 2018 9:57 am

Re: TreeView Node Selection

Post by JaimeBou » Fri Jun 21, 2019 5:07 am

I figured out in the demo code that this code provides the functionality that I am seeking:

$treeview1.add_NodeMouseClick($treeview1_NodeMouseClick)

What else besides the TreeNodeMouseClickEventHandler do I need?

I added both parts but my checkboxes still do not check when selecting the node text.

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: TreeView Node Selection

Post by jvierra » Fri Jun 21, 2019 5:47 am

Why would you add that line. That is added automatically when you define an event in the designer.

I would suggest reading the manual for PSS. The first few pages will tell you how to create events and how to edit the generated code. It will also tell you how to use the toolbox and the control properties panel. Once you understand this the rest is easy. With PSS we do not need to write more than a few lines in any event.

Look at the events that have been added to the TreeView in the event properties tab. They will show you how the code is inserted and what it is supposed to do.

The documentation for the controls is on the right context menu of the toolbox.

User avatar
JaimeBou
Posts: 12
Joined: Fri Feb 09, 2018 9:57 am

Re: TreeView Node Selection

Post by JaimeBou » Fri Jun 21, 2019 6:19 am

I will review the documentation.

But I wanted to answer your question.

The code that I am working on was never started in PSS or coded in Designer.

Thanks.

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: TreeView Node Selection

Post by jvierra » Fri Jun 21, 2019 6:39 am

Why don't you use the designer? It makes forms much easier to work with.
You can also export the PSF to a PS1 to see how the code works.

User avatar
JaimeBou
Posts: 12
Joined: Fri Feb 09, 2018 9:57 am

Re: TreeView Node Selection

Post by JaimeBou » Fri Jun 21, 2019 9:56 am

I am still learning PSS. I am trying to get there.

I do have another question. I ran the debugger on your demo app and noticed that when I run the debugger on my app that the "System.Windows.Forms.TreeNodeMouseClickEventHandler" does not seem to trigger.

I added a Write-Host "EventHander Triggered." line before the "$_.Node.Checked = -not $_.Node.Checked" line and do not see it run.

Can you tell me how to further troubleshoot this issue?

Locked