Checked TreeViews.....

sohaib

Well-known member
Joined
Apr 30, 2005
Messages
64
Programming Experience
3-5
Hi guys!!!

I have a checked treeview with parents and child nodes i want to ask a very simple question regarding the checked box criteria....The problem is tht whenever parent node is selected the child nodes of that parent should be automatically selected and one is removed it should be removed from the parent node......As we have seen in some software installation packages.......

one thing is more in tht case tht i ve to add all selected nodes into the list so that i will perform another task on the basis of those selected nodes...

Can anyone help me in this regard..

It's really needed for my case and any other suggestion if anyone can give me to resolve this issue regarding parent and child checked boxes......

Hope for the positive response from u soon...

ok
bye
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,331
Location
Sydney, Australia
Programming Experience
10+
Use the TreeView.AfterCheck event handler like this:
VB.NET:
	[color=Blue]Private Sub[/color] TreeView1_AfterCheck([color=Blue]ByVal[/color] sender [color=Blue]As[/color] [color=Blue]Object[/color], [color=Blue]ByVal[/color] e [color=Blue]As[/color] System.Windows.Forms.TreeViewEventArgs) [color=Blue]Handles[/color] TreeView1.AfterCheck
		[color=Blue]If[/color] e.Node.Checked [color=Blue]Then[/color]
			[color=Blue]For Each[/color] childNode [color=Blue]As[/color] TreeNode [color=Blue]In[/color] e.Node.Nodes
				childNode.Checked = [color=Blue]True[/color]
			[color=Blue]Next[/color]
		[color=Blue]ElseIf Not[/color] e.Node.Parent [color=Blue]Is Nothing Then
[/color] 			e.Node.Parent.Checked = [color=Blue]False[/color]
		[color=Blue]End If[/color]
	[color=Blue]End Sub[/color]
Note that if your TreeView has more than 2 levels, checking a node will check all nodes at every level below that, and unchecking a node will uncheck its parent, grandparent, great-grandparent and so on. You will have to refine the code if you don't want this to happen.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,331
Location
Sydney, Australia
Programming Experience
10+
I was perusing the Code Project web site and found a TreeView that supports tri-state check boxes. This sounds ideal for what you are doing. That way a parent can be checked if all its children are checked, unchecked if all its children are unchecked and in the third, indeterminate state when only some of its children are checked. Check it out here:

http://www.codeproject.com/vb/net/vbnettristatechkbox.asp
 

sohaib

Well-known member
Joined
Apr 30, 2005
Messages
64
Programming Experience
3-5
Hi jmcilhinney!!!

I dont know ur exact name anyways!!!

It's really work. Sir, thnx a lot....

Now i have a little bigger problem. tht's fine when a parent clicked childs are automatically selected but how we can add this information into the database???

I have to make tables with parent-child relationship?? or any other suggestion

I m making a project in which technical skills have to be selected in this scenario because they are sub-divided and every thing should be selected or clicked so how i can add into the database for future reference and for finding!!!!!
If i want to find that particular skill in all records so i have to search as well within whole database.... i know this looks very difficult.... but sir plz help and guide me in the right direction....

I hope for the positive response from u soon

Takecare, Bye
Sohaib
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,331
Location
Sydney, Australia
Programming Experience
10+
The relationships represented in the TreeView should almost undoubtedly be implemented in a database using a table for each level of the TreeView and a one-to-many relationship between the table fields. In it's simplest form, the parent table, perhaps called Category, would have a single column, perhaps called CategoryName. The child table, perhaps called Skill, would have two columns, perhaps called CategoryName and SkillName, with the two CategoryName columns being related. A slightly preferable approach, in my opinion, would be for the Category table to have CategoryID and CategoryName columns and the Skill table to have SkillID, CategoryID and SkillName columns, with the CategoryID columns being related.
 

sohaib

Well-known member
Joined
Apr 30, 2005
Messages
64
Programming Experience
3-5
Hi!!!

You are almost at the right direction but the problem is still there as u mentioned earlier in ur reply...

Can we do with Listview.... i mean select in treeview and then placed skills in listview with its experience as subitems; Can we add into the database in this way....

One thing u have given me a code for selecting parent and after clicking it it will select all its child but when deselecting it should deselect its child but it is not happpening in ur case....

Can u give a better solution with listview using treeview as well i hope so u understand what i mean...

ok
Bye
 

sohaib

Well-known member
Joined
Apr 30, 2005
Messages
64
Programming Experience
3-5
Hi Kulrom!!!

Everything is going fine but i have already told my whole scenario in the "Attaching Files" Thread in VB.Net Discussion Forum so plz read it and tell me abt few good suggestions....


And if u think selection from Treeview and then added into the listview, it is not happening now im trying but still no result...It is adding no doubt abt it but when i want to add parent field it should select whole child(it is happening) but the parent field(text) should only be added into the listview but in the case of child the child field only be selected and added......and the time of deselection the same scenario will occur i mean deselection of parent node should deselect all of its child and remove its name from listview and deselection of child field should deselect only child node and remove its text from listview...

This is the scenario on which i m working on... I hope u understand my problem...

Hope for the positive response from u soon....

ok
Bye
 

bwship

New member
Joined
May 24, 2006
Messages
1
Programming Experience
5-10
Almost perfect

One thing your code does not do, is if you check a child node, and all other child nodes are now checked, I would like to have the parent become automatically checked as well, and the check would hav eto then go up the line, and if the parent and all its brother nodes are now checked then the grandparent would become checked as well, etc. Anyone know of the code for that?
 

jacky.goy

Active member
Joined
Sep 19, 2006
Messages
38
Programming Experience
1-3
bwship,

Below are some example, you may modify the codes to suit your solution:-


VB.NET:
Public validated as Boolean
 
Private Sub TViewARModTools_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TViewARModTools.AfterCheck
If validated = True Then
Exit Sub
End If
 
Select Case e.Node.Checked
Case Is = True
If e.Node.Checked = True Then
For Each childNode As TreeNode In e.Node.Nodes
childNode.Checked = True
Next
End If
validateChildCheckedAll(e.Node)
 
Case Is = False
If e.Node.Level = 0 Then
If e.Node.Checked = False Then
validated = True
For Each childNode As TreeNode In e.Node.Nodes
childNode.Checked = False
Next
End If
Else
If Not e.Node.Parent Is Nothing Then
validated = True
e.Node.Parent.Checked = False
End If
End If
End Select
BtnOK.Enabled = True
validated = False
End Sub

VB.NET:
Private Sub validateChildCheckedAll(ByVal e As TreeNode)
Dim myNode As TreeNode = e
Dim parentNodeClicked As TreeNode = e.Parent
Dim nodeCount, nodeCountClicked As Integer
Select Case myNode.Level
Case Is = 0
Case Is = 1
For Each childNode As TreeNode In e.Parent.Nodes
nodeCount = e.Parent.Nodes.Count
If childNode.Checked = True Then
nodeCountClicked = nodeCountClicked + 1
Else
Exit For
End If
Next
If nodeCountClicked = nodeCount Then
validated = True
parentNodeClicked.Checked = True
End If
End Select
End Sub
 
Top Bottom