Notifications
Clear all

[Closed] Treeview node checking on formload

Hi,

I have been trying to do this for almost a month now, and it is so funny that still i haven’t got an answer.
Here i have two functions that reads all the nodes in the treeview and prints it onto the listener exactly in the same order as you see in the treeview. I have the checkboxes property turned on for my treeview. What i am trying to do is to check the root nodes in the treeview on form load. There is not much happening in this code but my intention is to get the node checking function to work and apply this to a different scenario. Does anybody see anything wrong this.

fn tvChildOfRootNodes nd=
(
for i=0 to nd.Nodes.count-1 do
(
Print nd.nodes.item[i].Text
nd.checked=true
tvChildOfRootNodes nd.nodes.item[i]
)
)
fn tvRootNodes=
(
local tn =dotNetObject “System.windows.forms.TreeNode”
for i=0 to tvObjs.nodes.count-1 do
(
print tvObjs.nodes.item[i].text
tn=tvObjs.nodes.item[i]
tn.checked=true
tvChildOfRootNodes tn
)
)

Thanks for a having a look at this post

Sumesh

14 Replies

Looking at the code, I assume that having this line in the “childOfRootnodes” function would check all nodes except those on the lowest level:

for i=0 to nd.Nodes.count-1 do
(
 ...
 nd.checked=true
 ...
)

If you just want to check the rootnodes, then you don’t have to recurse through the childnodes at all.

Btw, in the RootNodes function, you don’t need to initialize the local tn with a new treenode:

local tn =dotNetObject "System.windows.forms.TreeNode"

can be replaced with:

local tn

Thanks Janssen for the reply.

I would have been happy if this code checked something in the treeview control. The lines “nd.checked =true” and “tn.checked=true” are doing nothing in the script. As you said they dont check anything. That is where it all starts from, dont have any idea wht is happening.

My intention is to apply this piece of code when the form loads, so that my treeview control will have the nodes checked accordingly with the objects selected on the scene.

The following code only prints the name of the selected objects on the scene. Now i want to add the tn.checked=true and nd.checked=true to both the functions to make the nodes check instead of printing it.

fn tvChildOfRootNodes nd selObj= – Retrieving values from the tvRootNodes fn
(
for i=0 to nd.nodes.count-1 do
(
if selObj==nd.nodes.item[i].text then print nd.nodes.item[i].text–works
tvChildOfRootNodes nd.nodes.item[i] selObj
)
)
fn tvRootNodes=
(
local tn =dotNetObject “System.windows.forms.TreeNode”
for s=1 to selection.count do– Lopping thru the selection array
(
for i=0 to tvObjs.nodes.count-1 do– Looping theu the treeview root nodes
(
if selection[s].name==tvObjs.nodes.item[i].text then print tvObjs.nodes.item[i].text–works
tn=tvObjs.nodes.item[i]
tvChildOfRootNodes tn selection[s].name –Calling the fn with the treenode and the 1st obj of the selection array.
)
)
)

Thanks
Sumesh

Hello,

Does anybody have a solution to this one?

Thanks
Sumesh

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

where is the question?
could you use the <code>code</code> tag to define your code, please?

Hi Denis,

I have two functions that runs when the form loads, which is used for populating the treeview control with the objects on the scene along with their hierarchical information. Those functions also has code for colouring the nodes based on whether they are hidden or frozen in the scene. Everything works really fine.

The treeview control is also having checkboxes attached to each node. Now the two functions that are posted below are for checking the nodes based on whether they are selected on the scene. The point is to have the nodes checked on the form load, if those objects are selected on the scene.The “tn.checked=true” and the “nd.checked=true” line doesn’t work at all. I have tried printing the nodes to the listener just to check on my logic, everything seems to work fine except it doesn’t check the nodes.


fn tvChildOfRootNodes nd selObj=   -- Retrieving values from the tvRootNodes fn 
 (
 	for i=0 to nd.nodes.count-1 do
 	(
  	   if selObj==nd.nodes.item[i].text then 
	   (
		  nd.checked=true
	   )
	tvChildOfRootNodes nd.nodes.item[i] selObj
	)
  )
 fn tvRootNodes=
 (
    local tn =dotNetObject "System.windows.forms.TreeNode"
    for s=1 to selection.count do	 --   Lopping thru the selection  array
    (
  	  for i=0 to tvObjs.nodes.count-1 do-- Looping theu the treeview root nodes
  	  (
		  if selection[s].name==tvObjs.nodes.item[i].text then 
		  (
			tn=tvObjs.nodes.item[i] 
			tn.checked=true
		  )
	 tvChildOfRootNodes tn selection[s].name 
	  )	 
   )
)

Hoping that you could find a solution on this.

Thanks Denis

Sumesh

1 Reply
(@plastic)
Joined: 11 months ago

Posts: 0

Every time you call the function tvRootNodes() a new dotNetObject is generated.
The for loop runs on this empty TreeNode.
I guess you want to use an existing TreeNode instead.

I’d suggest to use the TreeNodeCollection.Find method to get the selected treenodes. This function basically does the same thing as you do, but its built in .NET. So that will make you own code simpler.
Here’s an example of how that could look: (haven’t tested it, but just to give you an idea)

function checkSelectedNodes = 
(
 local findNodeFn = tvObj.Nodes.Find; --Store the function in a local var to avoid having to look it up each time in the loop.
 local foundNoudes;
 for selNode in selection do
 (
  foundNodes = findNodeFn selNode.name true;
  if foundNodes != undefined AND foundNodes.Length > 0 do
  (
    for i = 0 to foundNodes.Length - 1 do
     foundNodes[i].Checked = true;
  )
 )
)

You could make a little shortcut in there if you’re sure that you will always have just one treenode for each node in the scene. In that case you don’t have to loop through the foundNodes array, but simply check if its length equals 1 and check just element 0.

Thanks Janssen.
I tried your code, but its not working. i also tried some tweaks on it.


 foundNodes = findNodeFn selNode.name true;

Right after this line, i tried printing the value of foundNodes to the listener with the objects selected on the scene. Its not printing anything.

Still working on it…

Thanks
Sumesh

1 Reply
(@pjanssen)
Joined: 11 months ago

Posts: 0

If it’s not printing anything, then it’s probably not even being called. I just tried the code I wrote, there was an error in there indeed (I forgot that .NET arrays are converted to maxscript arrays automatically.)
Here’s a bit of code that works fine:

(
	rollout testRollout "test"
	(
		dotnetcontrol tvObj "System.Windows.Forms.TreeView" height:250;
	
		function checkSelectedNodes = 
		(
			local findNodeFn = tvObj.Nodes.Find; --Store the function in a local var to avoid having to look it up each time in the loop.
			local foundNoudes;
			for selNode in selection do
			(
				foundNodes = findNodeFn ((GetHandleByAnim selNode) as string) true;
				if foundNodes != undefined AND foundNodes.count == 1 do
					foundNodes[1].Checked = true;
			)
		)
	)
	
	CreateDialog testRollout 200 260;
	testRollout.tvObj.CheckBoxes = true;
	for o in objects do testRollout.tvObj.Nodes.Add ((GetHandleByAnim o) as string) o.name;
	testRollout.checkSelectedNodes();
)

To avoid issues with duplicate node names, I used the node handle as the key for the treenode. (GetHandleByAnim)

I tried your code, its working perfectly. But somehow when i paste your code onto mine it doesn’t check anything. It doesn’t pop any errors also.
I see that you declared your treeview control in this way

dotnetcontrol tvObj "System.Windows.Forms.TreeView" height:250;

but mine is like this

local tvObjs = dotNetObject "System.windows.forms.treeview"

Does dotNetControl or dotNetObject make any difference.
Thanks Janssen for the help again

Thanks
Sumesh

you need to look up global and local variable scope basics.

1 Reply
(@sumchans)
Joined: 11 months ago

Posts: 0

Can you explain a bit more. How it will help me here.

Thanks Marc

Sumesh

well, look up “scope of variables” in the maxscript help.

there are global and local variables/objects.

it’s very important that you understand how it works.

Page 1 / 2