Page 1 of 2 12>
Topic Options
#204683 - 2012-04-10 03:43 PM Kf.Classic: How do I determine if TreeNode has Child Nodes ?l
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Guys,

again having trouble with my current project.
Can't tell when looping through a TreeViews Node Collection if the current Node has child nodes in the next level (my TreeView will have 3 levels of Nodes, all checkable)

here's what I tried (one of Shawns samples adapted a bit to simulate more than one level of Nodes, totally unstylish, what I can't find is in the comment in the only function of the script)

 Code:
Break On
global $l1, $l2
$= setoption("NoVarsInStrings","ON")
$System = CreateObject("Kixtart.System")

$f = split(readprofilestring(@scriptdir + "\Form.ini", "Form", ""),chr(10))

$Form = $System.Form()
$Form.Size = 640,480
$Form.Center()

$Form.ToolGroupBox = $Form.GroupBox()
$Form.ToolGroupBox.Height = 30
$Form.ToolGroupBox.Dock = 1

$Form.StatusGroupBox = $Form.GroupBox()
$Form.StatusGroupBox.Height = 20
$Form.StatusGroupBox.Dock = 2

$Form.TreeView1 = $Form.Controls.Add("TreeView")
$Form.TreeView1.Width = $Form.ClientWidth / 4
$Form.TreeView1.Dock = 3
$Form.TreeView1.ShowLines			= 1
$Form.TreeView1.ShowPlusMinus		= 1
$Form.TreeView1.ShowRootLines		= 1
$Form.TreeView1.CheckBoxes			= 1
$Form.TreeView1.OnClick             = "treeView_Click()"
$Form.TreeView1.OnAfterSelect		= "treeView_Click()"

$Form.Splitter1 = $Form.Splitter()
$Form.Splitter1.Dock = 3

$Form.TextBox1 = $Form.TextBox()
$Form.TextBox1.Multiline = 1
$Form.TextBox1.Dock = 5

for each $e in $f
	if $e
		$l1 = $Form.TreeView1.Nodes.Add($e)
		$l2 = $Form.TreeView1.Nodes(ascan($f,$e)).Nodes.Add($e+" - " + readprofilestring(@scriptdir + "\Form.ini", "Form", $e))
	endif
next

$Form.Show
While $Form.Visible
 $=Execute($System.Application.DoEvents)
Loop

Exit 1

function treeView_Click()
	dim $checkedNodes
	for $i = 0 to $Form.Treeview1.Nodes.Count - 1
		if $Form.Treeview1.Nodes($i).checked
			$checkedNodes = $checkedNodes + $Form.Treeview1.Nodes($i).FullPath + @crlf
		endif
		; here would go something like
		; if $Form.Treeview1.Nodes($i).HasChild ..
	next
	if $checkedNodes
		$Form.TextBox1.Text = $checkedNodes
	endif
endfunction


attached is the file I used to get input for the TreeView..

As you will see only the first level of checked Nodes will get displayed right of the splitter \:\(



Attachments
Form.ini (513 downloads)
Description:




Edited by Jochen (2012-04-10 03:43 PM)
_________________________



Top
#204684 - 2012-04-10 03:53 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Jochen]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
More Background:

as my final treeView will have 3 levels I want that if one checks a Node all child Nodes will get checked as well .. which I fear I will have to do programmatically \:D

oh, and leaving out the condition in the update of TextBox will make it even clearer:

 Code:
function treeView_Click()
	dim $checkedNodes
	for $i = 0 to $Form.Treeview1.Nodes.Count - 1
		if $Form.Treeview1.Nodes($i).checked
			$checkedNodes = $checkedNodes + $Form.Treeview1.Nodes($i).FullPath + @crlf
		endif
		; here would go something like
		; if $Form.Treeview1.Nodes($i).HasChild ..
	next
	$Form.TextBox1.Text = $checkedNodes
endfunction


Edited by Jochen (2012-04-10 03:58 PM)
Edit Reason: code adapted
_________________________



Top
#204689 - 2012-04-10 07:26 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Jochen]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
possible workaround could be the .Tag property in combination with the .GetNodeAT(X, Y) method (undocumented ?!!?)

But on the other side, I may have up to 5000+ Nodes in 3 levels of Nodes. Could be a performance nightmare.


Edited by Jochen (2012-04-10 07:56 PM)
_________________________



Top
#204697 - 2012-04-11 04:21 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Jochen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
I think something like that .Tag may be the only way. I cannot seem to find any other properties that help.
Top
#204698 - 2012-04-11 04:34 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: ShaneEP]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
now now... wait just a minute, let me take a lookie :P
_________________________
!

download KiXnet

Top
#204699 - 2012-04-11 04:40 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
there you go:
 Code:
function treeView_Click()
	dim $checkedNodes, $i, $is
	for $i = 0 to $Form.Treeview1.Nodes.Count - 1
		if $Form.Treeview1.Nodes($i).checked
			$checkedNodes = $checkedNodes + $Form.Treeview1.Nodes($i).FullPath + @crlf
		endif
		for $is = 0 to $Form.Treeview1.Nodes($i).Nodes.Count - 1
			if $Form.Treeview1.Nodes($i).Nodes($is).checked
				$checkedNodes = $checkedNodes + $Form.Treeview1.Nodes($i).Nodes($is).FullPath + @crlf
			endif
		next
	next
	if $checkedNodes
		$Form.TextBox1.Text = $checkedNodes
	endif
endfunction
_________________________
!

download KiXnet

Top
#204700 - 2012-04-11 04:55 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
checkstate would be a nice addition but couldn't get it to work. I guess it's not supported on classic.
_________________________
!

download KiXnet

Top
#204701 - 2012-04-11 07:32 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Lonkero]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
 Quote:
I want that if one checks a Node all child Nodes will get checked as well

Maybe I'm missing the real problem, but this still does not work with your new UDF. At least not on my system here.

Top
#204702 - 2012-04-11 07:36 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Maybe something like this?...
 Code:
function treeView_Click()
	dim $checkedNodes, $i, $is
	for $i = 0 to $Form.Treeview1.Nodes.Count - 1
		if $Form.Treeview1.Nodes($i).checked
			$checkedNodes = $checkedNodes + $Form.Treeview1.Nodes($i).FullPath + @crlf
		endif
		for $is = 0 to $Form.Treeview1.Nodes($i).Nodes.Count - 1
			if $Form.Treeview1.Nodes($i).checked
				$Form.Treeview1.Nodes($i).Nodes($is).checked = 1
				$checkedNodes = $checkedNodes + $Form.Treeview1.Nodes($i).Nodes($is).FullPath + @crlf
			else
				$Form.Treeview1.Nodes($i).Nodes($is).checked = 0
			endif
		next
	next
	if $checkedNodes
		$Form.TextBox1.Text = $checkedNodes
	endif
endfunction

Top
#204703 - 2012-04-11 11:32 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: ShaneEP]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh, I missed that reply comment...

but...
there is a minor issue with your approach.
if someone checks a box on third level and the upper level is not checked, his check will be wiped out.
this is also why I wanted to have the tri-state checkbox action in there.

maybe something like the following.
a) if all childnodes checked, check also the parent
b) if parent is de-checked, uncheck all children
c) if parent is checked, check all children

note that the example ini doesn't really work for testing anymore as we only have 1 child in every node.
 Code:
found a bug. the corrected code in next post.


_________________________
!

download KiXnet

Top
#204704 - 2012-04-11 11:37 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
the above code corrected here:
 Code:
function treeView_Click()
	dim $checkedNodes, $i, $is
	for $i = 0 to $Form.Treeview1.Nodes.Count - 1
		if $Form.Treeview1.Nodes($i).checked
			$checkedNodes = $checkedNodes + $Form.Treeview1.Nodes($i).FullPath + @crlf
			for $is = 0 to $Form.Treeview1.Nodes($i).Nodes.Count - 1
				$Form.Treeview1.Nodes($i).Nodes($is).checked = 1
			next
			$Form.Treeview1.Nodes($i).tag = 1
		else
			if $Form.Treeview1.Nodes($i).tag = 1
				for $is = 0 to $Form.Treeview1.Nodes($i).Nodes.Count - 1
					$Form.Treeview1.Nodes($i).Nodes($is).checked = 0
				next
				$Form.Treeview1.Nodes($i).tag = 0
			endif
		endif
		dim $nc
		$nc=1
		for $is = 0 to $Form.Treeview1.Nodes($i).Nodes.Count - 1
			if $Form.Treeview1.Nodes($i).Nodes($is).checked
				$checkedNodes = $checkedNodes + $Form.Treeview1.Nodes($i).Nodes($is).FullPath + @crlf
			else
				$nc=0
			endif
		next
		if $nc=1 and not $Form.Treeview1.Nodes($i).checked
			$Form.Treeview1.Nodes($i).checked = 1
			$Form.Treeview1.Nodes($i).tag = 1
		endif
	next
	if $checkedNodes
		$Form.TextBox1.Text = $checkedNodes
	endif
endfunction


the missing line caused unchecking a parent impossible in situation when it was checked because children all were checked. now it seems to work as expected.
_________________________
!

download KiXnet

Top
#204705 - 2012-04-12 01:35 AM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Lonkero]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Still seems a little out of whack. I added this line to the form code to get a 3rd level.
 Code:
		$l3 = $l2.Nodes.Add($e+" - " + readprofilestring(@scriptdir + "\Form.ini", "Form", $e))

Top
#204706 - 2012-04-12 01:36 AM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Clicking the parent only checks the 2nd level, not the third. Also clicking just the 2nd level will check the parent and itself but not the 3rd.
Top
#204707 - 2012-04-12 02:15 AM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: ShaneEP]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
ok, now as we have a deeper construct, if this is what is he wanting, the we need to modify the code. I thought his required level was demonstrated by his post.
brb...
_________________________
!

download KiXnet

Top
#204708 - 2012-04-12 03:07 AM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
this is weird.
not really sure how it happens but sometimes all checkboxes get checked without any of them actually being checked (just clicking on the treeview makes it happen). don't make no sense.
_________________________
!

download KiXnet

Top
#204709 - 2012-04-12 03:51 AM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
blaah. harder than expected.
untagging to infinite levels doesn't work yet...
_________________________
!

download KiXnet

Top
#204710 - 2012-04-12 04:06 AM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
here you go my friend.
 Code:
function treeView_Click()
		$trash = CheckedNodes($Form.Treeview1)
		$Form.TextBox1.Text = CheckedNodes($Form.Treeview1)
endfunction

Function CheckedNodes($parent)
	dim $i, $nc, $no
	for $i = 0 to $parent.Nodes.Count - 1
		if $parent.tag=1 and not $parent.checked
			$parent.Nodes($i).checked = 0
		endif
		if $parent.checked and $parent.tag<>1
			$parent.tag = 1
			$parent.Nodes($i).checked = 1
		endif
		$checkedNodes=$checkedNodes+CheckedNodes($parent.Nodes($i))
		if $parent.Nodes($i).checked
			$checkedNodes = $checkedNodes + $parent.Nodes($i).FullPath + @crlf
			$no=$no+1
			$nc=$nc+1
		else
			$no=$no+1
		endif
	next
	if $nc=$no and $no>0 and not $parent.checked and $parent.tag<>1
		$parent.checked = 1
		$parent.tag = 1
	else
		if $nc=0 and $no>0 and $parent.checked and $parent.tag=1
			$parent.checked = 0
			$parent.tag = 0
		endif
	endif
	if $parent.tag=1 and not $parent.checked
		$parent.tag=0
	endif
EndFunction
_________________________
!

download KiXnet

Top
#204727 - 2012-04-13 06:47 AM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Lonkero]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Wow guys,

always a pleasure to post my questions here, as I know they will be answered or there is no answer \:\)

Will dig into what you both have done and report back what I took with me.
Probably in the week after next because my plane is due to start anytime soon now and I don't know if they already invented free WiFi on Mallorca since last year this time ;\)
_________________________



Top
#204728 - 2012-04-13 06:51 AM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Jochen]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Had to try, could not resist!

Excellent! Now, let's see what this does to the performance with 5100+ Nodes

See you next week or the week after that.
_________________________



Top
#204733 - 2012-04-13 04:10 PM Re: Kf.Classic: How do I determine if TreeNode has Child Nodes ?l [Re: Jochen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
5100+?
it might have some delays :p

for that purpose, there is actually "easier" way to accomplish this :p

but, let us know, if it is worth coding a faster solution.
_________________________
!

download KiXnet

Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
2 registered (morganw, mole) and 414 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.073 seconds in which 0.022 seconds were spent on a total of 15 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org