p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB.NET 2002/2003 Basics (http://p2p.wrox.com/forumdisplay.php?f=76)
-   -   Treeview Manipulation (http://p2p.wrox.com/showthread.php?t=27176)

reyboy March 22nd, 2005 05:54 AM

Treeview Manipulation
1. We have selectedNode but how do we get the checked node that wasn't Selected?

2. How do we get the parent node of the checked node?

3. How do we not allow a parent node uncheked whose child is checked?

Thank in advance

Proud To Be Pinoy

BrianWren March 22nd, 2005 12:02 PM

I'm guessing, but I believe you are going to have to “walk the tree,” recursively following each branch, and checking the properties of each node.

I believe each node but the root node has a .Parent property.

It is likely to be easiest to just keep track of all changes to the tree (storing values in variables to represent the tree), starting from a known position when the program initially runs.
Then you can permit (or not) checking a node based on the information you have stored, rather than through “reading” the tree all over again.

What does it mean to be Pinoy?

katsarosj March 22nd, 2005 04:59 PM

The Treeview has a BeforeSelect event where you can place a for...next loop to scan the nodes.
You can get the parent by using e.Node.Parent (as Brian stated) which is passed in the Treeview arguments. You can look for a checked child like this:

If e.Node.Nodes.Count >= 1 And e.Node.Checked = True Then
     Dim tnode As New TreeNode
     For Each tnode In e.Node.Nodes
         If tnode.Checked Then
             MessageBox.Show("Child is checked.")
             e.Cancel = True 'cancels the check event
             Exit For 'exit the loop when one is found
         End If
End If

This should get you started, but as Brian said it needs to be recursive in order to scan nodes deeper than 1 level.


P.S. - Pinoy means Filipino.

reyboy March 22nd, 2005 09:15 PM

thanks guys, Ill try it out.

Proud To Be Pinoy

reyboy March 23rd, 2005 05:03 AM

The code was ok except for the "e.Cancel" part, I treeview eventargs doesnt support it.
Mine is VB.Net Std 2002 with SP2(?)

How can I add "e.cancel" because it just loops till an exception?

Proud To Be Pinoy

katsarosj March 23rd, 2005 09:55 AM

What exception are you getting? The code that I gave you should exit the loop when a check is found on a child node. Are you sure that the exception is not caused by something executing after you exit the loop?


reyboy March 28th, 2005 01:02 AM

My problem is that my treeview in VB.Net 2002 Std Ed. doesn't have "e.cancel".

After I type the "." operator I cant find the "Cancel" Method

Proud To Be Pinoy

katsarosj March 28th, 2005 09:43 AM

The e.Cancel essentially cancels out the check change without additional code by you. Since, apparently, .Net 2002 doesn't support this you can simply set "e.Node.Checked = True" and exit the loop.


All times are GMT -4. The time now is 05:16 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.