question

Shahaba-9505 avatar image
0 Votes"
Shahaba-9505 asked DewayneBasnett-7583 answered

how to Check All and Uncheck All in TreeView1 By Click on Button1 in vb.net?

Hi all
how to Check All and Uncheck All in TreeView1 By Click on Button1 in vb.net?
See This Pic and help me
thanks all
239545-1.png


dotnet-visual-basic
1.png (6.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
1 Vote"
cooldadtx answered Viorel-1 commented

Unfortunately there is no easy way to do that other than to manually enumerate the children. The easiest approach is to listen for the AfterCheck event on the treeview. When it fires then enumerate the child nodes using Nodes property and check/uncheck each child. Since you've hooked the event that would recursively call the handler for each child.

Public Class Form1
    Private Sub OnAfterCheck(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterCheck
        For Each child As TreeNode In e.Node.Nodes
            child.Checked = e.Node.Checked
        Next
    End Sub
End Class


If you wanted to make this reusable then you could wrap this logic into a derived TreeView class.

Note that this might not perform well if the treeview is large and it doesn't handle virtual items.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much for your answer.
I am a beginner, can you give an example?

239499-1.png


0 Votes 0 ·
1.png (10.9 KiB)

If you're a beginner then just use my original code. Creating a derived type would be an advanced concept. Keep it simple. But if you're interested it would look something like this.

Public Class MyTreeView
    Inherits System.Windows.Forms.TreeView

    Protected Overrides Sub OnAfterCheck(e As TreeViewEventArgs)
        MyBase.OnAfterCheck(e)

        For Each child As TreeNode In e.Node.Nodes
            child.Checked = e.Node.Checked
        Next
    End Sub

End Class


This eliminates the need for handling the AfterCheck event in the form. But you'd need to switch from using TreeView to MyTreeView in the designer.

0 Votes 0 ·

Do you still need the Button1?


0 Votes 0 ·
DewayneBasnett-7583 avatar image
1 Vote"
DewayneBasnett-7583 answered

Give this a try,

     Private Sub btnCheckAll_Click(sender As Object, e As EventArgs) Handles btnCheckAll.Click
         SetCheckState(TreeView1, True)
     End Sub
    
     Private Sub btnUn_Click(sender As Object, e As EventArgs) Handles btnUn.Click
         SetCheckState(TreeView1, False)
     End Sub
    
     Private Sub SetCheckState(tv As TreeView, checkState As Boolean)
         SetNode(tv.Nodes(0), checkState)
     End Sub
    
     Private Sub SetNode(Node As TreeNode, checkState As Boolean)
         Node.Checked = checkState
         ' Node.EnsureVisible() ' for debug
         For Each nd As TreeNode In Node.Nodes
             SetNode(nd, checkState)
         Next
     End Sub


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.