TreeView Node Selection

Ask your 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.
This topic is 4 years and 9 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

TreeView Node Selection

Post by JaimeBou »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TreeView Node Selection

Post by jvierra »

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 203 times
User avatar
JaimeBou
Posts: 27
Last visit: Wed Jan 31, 2024 8:52 am
Has voted: 1 time

Re: TreeView Node Selection

Post by JaimeBou »

Thank you. I will review the demo file.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TreeView Node Selection

Post by jvierra »

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: 27
Last visit: Wed Jan 31, 2024 8:52 am
Has voted: 1 time

Re: TreeView Node Selection

Post by JaimeBou »

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: 27
Last visit: Wed Jan 31, 2024 8:52 am
Has voted: 1 time

Re: TreeView Node Selection

Post by JaimeBou »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TreeView Node Selection

Post by jvierra »

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: 27
Last visit: Wed Jan 31, 2024 8:52 am
Has voted: 1 time

Re: TreeView Node Selection

Post by JaimeBou »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TreeView Node Selection

Post by jvierra »

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: 27
Last visit: Wed Jan 31, 2024 8:52 am
Has voted: 1 time

Re: TreeView Node Selection

Post by JaimeBou »

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?
This topic is 4 years and 9 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