Link to home
Start Free TrialLog in
Avatar of IT_Steve
IT_Steve

asked on

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

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?
Avatar of Fahad Mukhtar
Fahad Mukhtar
Flag of Pakistan image

TreeView1.SelectedNode = TreeView1.Nodes(TreeView1.Nodes.Count - 1)
MsgBox(TreeView1.SelectedNode.Text)
MsgBox(TreeView1.SelectedNode.Checked)
or better not assign it to selected node
MsgBox(TreeView1.Nodes(TreeView1.Nodes.Count - 1).Text)
MsgBox(TreeView1.Nodes(TreeView1.Nodes.Count - 1).Checked)
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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)
Avatar of IT_Steve
IT_Steve

ASKER

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.
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