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?
or better not assign it to selected node
MsgBox(TreeView1.Nodes(Tre eView1.Nod es.Count - 1).Text)
MsgBox(TreeView1.Nodes(Tre eView1.Nod es.Count - 1).Checked)
MsgBox(TreeView1.Nodes(Tre
MsgBox(TreeView1.Nodes(Tre
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(nod
End If
Return lastnode
End Function
To use this function and get the last node :
MsgBox(GetLastNode(TreeVie
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.
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.
ASKER
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.TreeV iewEventAr gs) 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.No de, 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
Private Sub trvBrands_AfterCheck(ByVal
ByVal e As System.Windows.Forms.TreeV
'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.No
'else at last node so see if model ID from datatable matches node.text
Else
For Each drw In dasGV.Tables("TreeSamples"
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
End If
Next node
For Each drw In dasGV.Tables("TreeSamples"
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
MsgBox(TreeView1.SelectedN
MsgBox(TreeView1.SelectedN