Unable to cast Object Treeview.node

Mr_Gee

Active Member
Licensed User
I've checked all the examples on the forum which included treeview, but all examples have the same issue (or appear to have it)

I'm writing an application where the user can select a file (just the path)
I tried to do this with FileOpen but that only allows me access to the memory card, Treeview was the next option, the examples on the forum all have the same problem , they give an error when I try to compile them:
"Unknown control type Use Control (Name,Type) instead"
Line XXX : Control(node).addnewnode (SubString(s,c,StrLength(s)-c))

changing this to Control(node,treeview).addnew....etc.
Seems to solve it in the EDI but when I run the compiled program I get the attached error..
(this is for the desktop version, the ppc version also has errors)

Any help is appreciated

Thanks
 

Attachments

Mr_Gee

Active Member
Licensed User
You are using the wrong type name I think

try changing to "node"

Control(node,node).addnew....etc.
Thanks agraham, but I also tried that, I got a similar error (attached)
I also tried Form, listbox and arraylist but no dice :-(
 

agraham

Expert
Licensed User
I think you have probably incorrectly changed a treeview control reference. Treeviews and nodes are different types of object. Look at the Object list in the Project Explorer at the right. You need to use the type name given there after the object name.

For a Node object
Control(node,node).addnew....etc.

For a TreeView object
Control(treeview,treeview).addnew....etc.
 

Mr_Gee

Active Member
Licensed User
Well thats the weird thing, i didn't change anything from the downloaded application, since i didn't know how to use treeview I downloaded an example
to see if i could figure out how it worked, but when i test the examples they give me errors, i'm trying to find out why so i can use treeview in my own application.

What happens if you try this example from Erel:
http://www.basic4ppc.com/forum/showpost.php?p=2871&postcount=4
When I run it using the IDE I get the Controle(node) message...

Do you get the same one?
Otherwise it could be my instalations (Desktop & PPC)
 

agraham

Expert
Licensed User
It works for me with this modified code

B4X:
Sub FindFolders (path, node) 'Finds all folders in a specific path
   WaitCursor(true)
   al1.clear
   DirSearch(al1,path)
   c = StrLength(path)
   For I =0 To al1.count - 1
      s=al1.item(i)
      Control(node, treeview).addnewnode (SubString(s,c,StrLength(s)-c))
   Next
   al1.Clear
   FileSearch(al1,path)
   For I =0 To al1.count - 1
      s=al1.item(i)
      node2.Value = Control(node,treeview).addnewnode (SubString(s,c,StrLength(s)-c))
      node2.ImageIndex = 2
      node2.SelectedImageIndex = 2
   Next
   Control(node,treeview).expandall
   WaitCursor(false)
End Sub
Line 40 errored as "invalid parameter" so I change"expand" to "expandall".

The code is misleading in that the parameter named node is actually a TreeView object! I needed three Control(node, treeview) edits.
 

specci48

Well-Known Member
Licensed User
Hi guys,

the reason for is error is the "optimized compilation".

During debug in the IDE or without "optimized compilation" basic4ppc casts the objects treeview and node as needed. So the example was always right up to version 5.8.

To speed up the executables with "optimized compilation" :)sign0060:) the programmer now has always to declare the right type (treeview or node), even you have to code some routines twice... :(


specci48
 

Mr_Gee

Active Member
Licensed User
Well I replaced the sub with your version, the error doesn't show in the IDE,
but after i compile it I get the Treeview.Treeview and Treeview.node error again
 

specci48

Well-Known Member
Licensed User
You can switch between the "old" and "optimized compilation" with menu File > Compile > Optimized Compilation.

If you do this you should only use one parameter in the control statement: control(node).


specci48
 

RandomCoder

Well-Known Member
Licensed User
Well I replaced the sub with your version, the error doesn't show in the IDE,
but after i compile it I get the Treeview.Treeview and Treeview.node error again
Just out of interest, try replacing all instances of Control(node,treeview) with Control(node,treeview.ControlRef)

I don't know if this will do the trick but it helped when I had a similar problem adding a context menu to a treeview control.

Regards,
RandomCoder
 

agraham

Expert
Licensed User
I've cracked it. I didn't look closely enough at what happens after the first call to Findfolders. First time parameter "node" is a TreeView, other times it is a Node. You need a conditional test to find out which. Note the type name of a Node as returned by ControlType is "Treeview.Node". You may need to try with a msgbox to see what is returned for other external controls.

B4X:
Sub FindFolders (path, node) 'Finds all folders in a specific path
   WaitCursor(true)
   al1.clear
   DirSearch(al1,path)
   c = StrLength(path)
   For I =0 To al1.count - 1
      s=al1.item(i)
      If ControlType(node) = "TreeView.Node" Then
         Control(node, node).addnewnode (SubString(s,c,StrLength(s)-c))
      Else
         Control(node, treeview).addnewnode (SubString(s,c,StrLength(s)-c))   
      End If
   Next
   al1.Clear
   FileSearch(al1,path)
   For I =0 To al1.count - 1
      s=al1.item(i)
      If ControlType(node) = "TreeView.Node" Then
         node2.Value = Control(node,node).addnewnode (SubString(s,c,StrLength(s)-c))
      Else
         node2.Value = Control(node,treeview).addnewnode (SubString(s,c,StrLength(s)-c))
      End If
      node2.ImageIndex = 2
      node2.SelectedImageIndex = 2
   Next
   If ControlType(node) = "TreeView.Node" Then
      Control(node,node).expandall
   Else
      Control(node,treeview).expandall
   End If
   WaitCursor(false)
End Sub
 

Mr_Gee

Active Member
Licensed User
@ specci48
I tried changing the compiler like you mentioned, and I got it working ( the original file)
So I incorporated that in my program and it worked! the only drawback is that the (or a) sub "remembers" the last folder opened and if used again it will not read the new folder that is clicked, so i cannot re-use the code to open another file, I think it probably has to do with the naming issue agraham found. I already tried to set most of the variables to "" but that didn't work...so thats next on my list :)

@agraham
great job, I'll try your version next, the compiled file is pretty big compared to the optimized version

@RandomCoder
Sure i'll try anything to get it working, I'm actually losing sleep over this ;-)

Last night I thought of making a standalone version of the folderchooser program, this could then write the location to an temp file and my program could then read it when it gets focus...

Thank you all for your help on this, I'll let you know if it works
 
Top