Solved

# How do I determine the last node in a TreeView to make decisions based on its checked state?

Posted on 2006-05-05
341 Views
What is the logic in VB.net to find the last node in a Treeview so I can do logic based on its checked state?
0
Question by:IT_Steve

LVL 19

Expert Comment

TreeView1.SelectedNode = TreeView1.Nodes(TreeView1.Nodes.Count - 1)
MsgBox(TreeView1.SelectedNode.Text)
MsgBox(TreeView1.SelectedNode.Checked)
0

LVL 19

Expert Comment

or better not assign it to selected node
MsgBox(TreeView1.Nodes(TreeView1.Nodes.Count - 1).Text)
MsgBox(TreeView1.Nodes(TreeView1.Nodes.Count - 1).Checked)
0

LVL 34

Accepted Solution

dim tn as treenode

tn = MyTreeView.nodes(MyTreeView.nodes.count - 1)

if tn.checked = true then
'do something
end if

I am assuming by "last node" you mean on the root level.  If you need to drill down to the last node of the last node then let me know.
0

LVL 19

Expert Comment

you can use this function recursivley to get the last node of the treeview
Private Function GetLastNode(ByRef node As TreeNode) As TreeNode
Dim lastnode As TreeNode = node
If node.Nodes.Count > 0 Then
lastnode = GetLastNode(node.Nodes(node.Nodes.Count - 1))
End If
Return lastnode
End Function

To use this function and get the last node :
MsgBox(GetLastNode(TreeView1.Nodes(TreeView1.Nodes.Count - 1)).Text)
0

Author Comment

I realize I asked the wrong question or at best, asked the wrong way. I'll try again. Let's say my TreeView looks similar to the following:

Brand A
ProductA1
ModelA1-1
ModelA1-2
...
ProductA2
ModelA2-1

Brand B
ProductB1
ModelB1-1
...

I want to find out if the checked state for each Model node is true or false. If it is true, I do something. Lets consider Brand A. For this example, I might only want to check ModelA1-2 and ProductA2 which automatically checks ModelA2-1. Eveything else remains unchecked. I hope this is a clearer example of what I am trying to ask. Thank you for your ideas.
0

Author Comment

BriCrowe answered the original question as asked and provided example code. I need to learn how to ask the right question. Using my example from above, I actually solved my problem the following way. Thank you for your help.

Private Sub trvBrands_AfterCheck(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvBrands.AfterCheck

'Declare dataRow variable so we can find rows in the Treesamples
'dataTable.
Dim drw As DataRow

' The code only executes if the user caused the checked state to change.
If e.Action <> TreeViewAction.Unknown Then

If e.Node.Nodes.Count > 0 Then
' Calls the CheckAllChildNodes method, passing in the current
' Checked value of the TreeNode whose checked state changed.
Me.CheckAllChildNodes(e.Node, e.Node.Checked)

'else at last node so see if model ID from datatable matches node.text
Else
For Each drw In dasGV.Tables("TreeSamples").Rows
If e.Node.Checked = True And e.Node.Text = drw("ModelID") Then
drw("Selected") = True
ElseIf e.Node.Checked = False And e.Node.Text = drw("ModelID") Then
drw("Selected") = False
End If
Next
End If

End If

End Sub

Private Sub CheckAllChildNodes(ByVal treeNode As TreeNode, _
ByVal nodeChecked As Boolean)
Dim node As TreeNode
Dim drw As DataRow

For Each node In treeNode.Nodes
node.Checked = nodeChecked
If node.Nodes.Count > 0 Then
' If the current node has child nodes, call the
'CheckAllChildsNodes method recursively.
Me.CheckAllChildNodes(node, nodeChecked)
End If
Next node

For Each drw In dasGV.Tables("TreeSamples").Rows
If node.Checked = True And node.Text = drw("ModelID") Then
drw("Selected") = True
ElseIf node.Checked = False And node.Text = drw("ModelID") Then
drw("Selected") = False
End If
Next
End Sub

Steve
0

## Featured Post

### Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…