VB.NET 2002/2003 BasicsFor coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
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.
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
Next
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.
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?
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.