-
Apr 21st, 2021, 03:48 PM
#1
Thread Starter
Lively Member
[RESOLVED] uncheck all parent nodes
I have a Treeview with some nodes.
Code:
TreeView1.CheckBoxes = True
TreeView1.Nodes.Add("Archive")
YearsPath.ForEach(Sub(wf)
TreeView1.Nodes(0).Nodes.Add(wf.Year)
End Sub)
TreeView1.ExpandAll()
I am looking for a way to uncheck all items when I click a button, so not with the Treeview aftercheck
Last edited by clausowitz; Apr 21st, 2021 at 05:37 PM.
Reason: changed my mind
-
Apr 21st, 2021, 07:06 PM
#2
Re: uncheck all parent nodes
Originally Posted by clausowitz
I have a Treeview with some nodes.
I am looking for a way to uncheck all items when I click a button, so not with the Treeview aftercheck
You would use the same code that performs the node unchecking, except you would call it from a Button.Click event handler rather than from the TreeView.AfterCheck event handler.
-
Apr 21st, 2021, 11:48 PM
#3
Re: uncheck all parent nodes
to clear all checked nodes, somthing like this
Code:
Private Sub uncheckTree(ByVal aTreeView As TreeView)
Dim n As TreeNode
For Each n In aTreeView.Nodes
n.Checked = False
Next
End Sub
'and call like this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
uncheckTree(TreeView1)
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Apr 22nd, 2021, 05:22 AM
#4
Thread Starter
Lively Member
Re: uncheck all parent nodes
Chris, the code works however what I needed was a way to change all StateImageIndex so the flag is replaced with a dotted line image. That way it shows no items were selected.
Attachment 181103
-
Apr 22nd, 2021, 06:16 AM
#5
Re: uncheck all parent nodes
Originally Posted by clausowitz
Chris, the code works however what I needed was a way to change all StateImageIndex so the flag is replaced with a dotted line image. That way it shows no items were selected.
Not sure what you're doing - the attachment isn't showing.
Do you have the TreeView.CheckBoxes property set to True or False?
If it is set to True and you also have a TreeView.StateImageList set up with the two images you want to toggle between at indexes 0 and 1, then the two images will be used instead of CheckBoxes; one image representing checked and the other representing unchecked. So Chris's code will work without modification. See the Remarks Section of the documentation for the TreeNode.StateImageIndex Property:
If the parent TreeView has check boxes enabled, the StateImageIndex is ignored and the node will display the first or second image in the StateImageList set on the parent TreeView to indicate an unchecked or checked state, respectively.
If the TreeView.CheckBoxes property set to False, then I imagine you'll still use the same approach as ChrisE showed (possibly with a bit of recursion thrown in to clear all the child nodes if required), only you'll need to set the TreeNode.StateImageIndex for each TreeNode to the required value instead of setting the node's Checked property.
Last edited by Inferrd; Apr 22nd, 2021 at 06:20 AM.
-
Apr 22nd, 2021, 06:44 AM
#6
Thread Starter
Lively Member
Re: uncheck all parent nodes
The TreeView.CheckBoxes property is set to True. TreeView.StateImageList is set up with the two images. Index 0 is a dotted line to make it appear empty. Index 1 is a flag.
Still the images don't change when I run Eric's code because it only unchecks the node I use. The images are used in children.
Last edited by clausowitz; Apr 22nd, 2021 at 06:51 AM.
-
Apr 22nd, 2021, 07:07 AM
#7
Thread Starter
Lively Member
Re: uncheck all parent nodes
I did some more research and got it to work using this code:
Code:
Private Sub UncheckTree(ByVal aTreeView As TreeView)
Dim n As TreeNode
For Each n In aTreeView.Nodes
'n.Checked = False
CheckAllChildNodes(n, False)
Next
End Sub
Private Sub CheckAllChildNodes(treeNode As TreeNode, nodeChecked As Boolean)
Dim node As TreeNode
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
End Sub
-
Apr 22nd, 2021, 10:58 AM
#8
Re: uncheck all parent nodes
Slightly different approach
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'test
Static chk As Boolean = False
chk = Not chk
SetAllCheckState(chk, TreeView1)
End Sub
Public Sub SetAllCheckState(state As Boolean, whTV As TreeView)
Dim ltn As List(Of TreeNode) = AllTreeNodes(whTV).ToList
For Each tn As TreeNode In ltn
tn.Checked = state
Next
End Sub
Private Iterator Function AllTreeNodes(whTV As TreeView) As IEnumerable(Of TreeNode)
Dim myEnumerator As IEnumerator = whTV.Nodes.GetEnumerator()
While myEnumerator.MoveNext()
For Each stn As TreeNode In ChildrenTN(DirectCast(myEnumerator.Current, TreeNode))
Yield stn
Next
End While
End Function
Private Function ChildrenTN(ChildrenOf As TreeNode) As List(Of TreeNode)
Dim rv As New List(Of TreeNode)
rv.Add(ChildrenOf)
For Each tn As TreeNode In ChildrenOf.Nodes
rv.AddRange(ChildrenTN(tn))
Next
Return rv
End Function
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|