Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,284 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,001 people online right now. Registration is fast and FREE... Join Now!




Treeview checkbox problem

 
Reply to this topicStart new topic

Treeview checkbox problem, I need help on the treeview checkbox

Stressine
18 Dec, 2008 - 07:29 PM
Post #1

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 10



Thanked: 1 times
My Contributions
Hi,

I'm trying to do treeview checkbox function in Visual Studio 2005 using VB.Net. I have successfully loaded the treeview checkbox as you can see in my attachment. But the problem starts here. I have designed the form before I inserted any code inside of it so my programming is mainly trying to connect to the instance in the design.

This are the following function I want to perform on the treeview checkbox:

1. When I select the ITAD checkbox, all the nodes under it will be checked automatically
2. When I select the Raw checkbox, all node under it will be selected.

When I search the internet on how to do this, I cannot understand most of them. Mainly because I'm new at this programming practice. So, I'm asking for any guidance on how to do this. stupid.gif

Any contribution and assistance is greatly appreciated. icon_up.gif

Thanks


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post


kasbaba
RE: Treeview Checkbox Problem
19 Dec, 2008 - 09:56 AM
Post #2

D.I.C Head
**

Joined: 3 Nov, 2008
Posts: 83



Thanked: 9 times
My Contributions
Hi,

I am not very good at Treeviews either, however I created this code. Check it and let me know if this works for you.


vb
    Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
If e.Node.Checked = True Then
CheckNode(e.Node.Nodes)
Else
UnCheckNode(e.Node.Nodes)
End If
End Sub

Public Function CheckNode(ByVal _nodeCollection As TreeNodeCollection) As TreeNode
Dim tmpNode As TreeNode
For Each _child As TreeNode In _nodeCollection
_child.Checked = True
tmpNode = CheckNode(_child.Nodes)
If Not tmpNode Is Nothing Then
Return tmpNode
End If
Next
Return Nothing
End Function

Public Function UnCheckNode(ByVal _nodeCollection As TreeNodeCollection) As TreeNode
Dim tmpNode As TreeNode
For Each _child As TreeNode In _nodeCollection
_child.Checked = False
tmpNode = UnCheckNode(_child.Nodes)
If Not tmpNode Is Nothing Then
Return tmpNode
End If
Next
Return Nothing
End Function


Hope this helps.

kasbaba
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Treeview Checkbox Problem
19 Dec, 2008 - 12:04 PM
Post #3

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,283



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
There are a couple ways to handle this situation, I will show both. The first (and probably my choice) would be to create a procedure that checks the status of the parent node and loop through each child node checking or unchecking them based on the status of the parent node. You can then call this procedure in the AfterCheck Event of the TreeView

CODE

Private Sub HandleChildNodeCheckedState(ByRef currentNode As TreeNode)
    Dim checkedStatus As Boolean = currentNode.Checked
    For Each childNode As TreeNode In currentNode.Nodes
        childNode.Checked = checkedStatus
        CheckChildNode(node)
    Next
End Sub

Private Sub myTree_AfterCheck(ByRef sender As Object, ByRef e As TreeViewEventArgs) Handles MyTree.AfterCheck
    HandleChildNodeCheckedState(e.Node)
End Sub


The second is to just handle it all in the [b]AfterCheck Event[/url], like this

vb

Private Sub myTree_AfterCheck(ByRef sender As Object, ByRef e As TreeViewEventArgs) Handles MyTree.AfterCheck
Dim currentNode As TreeNode
For Each currentNode In e.Node.Nodes
currentNode.Checked = e.Node.Checked
Next
End Sub


I prefer the first way because it also un-checks the child nodes if the parent is un-checked. Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

Stressine
RE: Treeview Checkbox Problem
21 Dec, 2008 - 11:54 PM
Post #4

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 10



Thanked: 1 times
My Contributions
I've got it running now. Thank you all for your help.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 03:03PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month