AAARRRH!
Please try this
win_main:tgadget=CreateWindow("Test",50,50,400,400,,WINDOW_TITLEBAR|WINDOW_STATUS)
tree_view:tgadget=CreateTreeView(10,10,200,320,win_main)
SetGadgetLayout tree_view,1,0,1,1
but_up:tgadget=CreateButton(" UP ",220,20,60,20,win_main)
but_down:tgadget=CreateButton("DOWN",220,40,60,20,win_main)
but_add:tgadget=CreateButton("ADD",220,80,60,20,win_main)
but_insert:tgadget=CreateButton("INSERT",220,100,60,20,win_main)
tree_root:tgadget=TreeViewRoot(tree_view)
tree_root:tgadget=AddTreeViewNode("Main",tree_root)
tree_root.context="Root"
tree_node_1:tgadget=AddTreeViewNode("Node 1",tree_root)
tree_node_2:tgadget=AddTreeViewNode("Node 2",tree_root)
tree_node_1.context="Node 1"
tree_node_2.context="Node 2"
For i=1 To 10
uno:tgadget=AddTreeViewNode("Test "+String(i),tree_node_1)
due:tgadget=AddTreeViewNode("Test "+String(i),tree_node_2)
uno.context="Test "+String(i)
due.context="Test "+String(i)
Next
Global nodo_ultimo:tgadget,nodo_root:tgadget
While True
WaitEvent
'Print CurrentEvent.ToString()
source:tgadget=tgadget(EventSource())
Select EventID()
' Rem
Case EVENT_GADGETSELECT
If source=tree_view
' Print "tree view"
gad:tgadget=tgadget(EventExtra())
' Print "> "+String(gad.context)
nodo_ultimo:Tgadget = SelectedTreeViewNode(tree_view)
nodo_root:tgadget=Tgadget(EventExtra())
Print "Last node "+String(nodo_ultimo.context)
Print "Nodo root : "+String(nodo_root.context)
SetStatusText win_main,"Select: "+String(nodo_ultimo.context)
posizione=FindPosition(nodo_ultimo)
Print "POS: "+posizione
End If
Case EVENT_GADGETACTION
If source=but_insert
If posizione<>-1
posizione=FindPosition(nodo_ultimo)
InsertTreeViewNode(posizione+1,"New",nodo_root.parent)
End If
End If
If source=but_add
If nodo_root<>Null AddTreeViewNode "New",nodo_root
End If
If source=but_up
If nodo_root<>Null MoveItem(-1)
End If
If source=but_down
If nodo_root<>Null MoveItem(1)
End If
Case EVENT_WINDOWCLOSE
End
End Select
Wend
Function MoveItem(dx:Int=0)
If dx=0 Return
If nodo_ultimo=Null Return
If nodo_root=Null Return
pos=FindPosition(nodo_ultimo)
parent:tgadget=nodo_ultimo.parent
If parent<>Null
Print "Parent "+String(parent.context)
End If
End Function
Function FindPosition(e:tgadget)
Local parent:tgadget=e.parent
i=-1
Print "PARENT: ["+String(parent.context)+"]"
If parent.kids=Null Return -1
For aa:tgadget=EachIn parent.kids
i=i+1
If aa=e Exit;Return i
'Print "I: "+i
Next
Return i
End Function
The Function FINDPOSITION *should* find the position of an item in the list of each parent.
Try to choose an element and press INSERT to watch what happens! Try many times because in the beginning seems it works perfectly, then...
Can someone tell me where is the error?