• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

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?
0
IT_Steve
Asked:
IT_Steve
  • 3
  • 2
1 Solution
 
Fahad MukhtarDistinguished EngineerCommented:
TreeView1.SelectedNode = TreeView1.Nodes(TreeView1.Nodes.Count - 1)
MsgBox(TreeView1.SelectedNode.Text)
MsgBox(TreeView1.SelectedNode.Checked)
0
 
Fahad MukhtarDistinguished EngineerCommented:
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
 
Brian CroweCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Fahad MukhtarDistinguished EngineerCommented:

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
 
IT_SteveAuthor Commented:
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
 
IT_SteveAuthor Commented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now