Page 1 of 2
help with TreeGadget
Posted: Wed Oct 26, 2005 5:28 pm
by josku_x
Hello!
I have been working around TreeGadgets... and now, I am trying to find out how I can add an item to a itemnode through a push button?????
I mean let's say I have a TreeGadget that looks similar to this:
and when a user presses a ButtonGadget(), then it would add a item to a specified node, like:
Code: Select all
FirstNode
SecondNode
|
|-My New Item
ThirdNode
Please help
Posted: Wed Oct 26, 2005 5:44 pm
by Straker
Not that I have been able to find. I have tried this in another language, but with the same control. I think its inherent in the MS treeview control.
I have even forced a left-click when the right-click is done but it still fails.
Posted: Wed Oct 26, 2005 5:48 pm
by josku_x
Straker, thanks for your help, I have even seem to not find any solution, so I decided to ask my other question of TreeGadgets, please check the first post, thank you very much
Posted: Wed Oct 26, 2005 6:01 pm
by Straker
Try experimenting with AddGadgetItem() in your Button Clicked event. You may have to use OpenTreeGadgetNode() and CloseTreeGadgetNode() to identify the parent node.
Posted: Wed Oct 26, 2005 6:03 pm
by josku_x
I tried AddGadgetItem(), doesn't help, and with OpentreeGadgetNode() it just creates a new node below my other nodes, I even tried to get the position of the node, but still nothing.
can this even be done?

Posted: Wed Oct 26, 2005 6:52 pm
by josku_x
still needing help!
please, Fred, have you anything?
Posted: Wed Oct 26, 2005 7:32 pm
by freak
I admit the current way of handling TreeGadget items is not very transparent.
Thats why we are trying to find a simpler and more intuitive way of handling
this for the next version.
Here is some example code on how it currently works. You have to
to use OpenTreeGadgetNode() on the item after which you want to add the child,
and then add a new item one position below that.
Code: Select all
#Button = 0
#Tree = 1
If OpenWindow(0, 0, 0, 300, 500, #PB_Window_SystemMenu|#PB_Window_ScreenCentered, "TreeGadget")
If CreateGadgetList(WindowID())
TreeGadget(#Tree, 5, 5, 290, 460)
ButtonGadget(#Button, 195, 470, 100, 25, "Add")
AddGadgetItem(#Tree, -1, "Item 0")
AddGadgetItem(#Tree, -1, "Item 1")
AddGadgetItem(#Tree, -1, "Item 2")
AddGadgetItem(#Tree, -1, "Item 3")
AddGadgetItem(#Tree, -1, "Item 4")
Repeat
Event= WaitWindowEvent()
If Event = #PB_Event_Gadget
If EventGadgetID() = #Button
; get the item that is selected
Item = GetGadgetState(#Tree)
; we must open the node that will be the parent of our new item
OpenTreeGadgetNode(#Tree, Item)
; the new item must be below the selected one, so add at position Item+1
AddGadgetItem(#Tree, Item+1, "New Item")
; close the node again..
CloseTreeGadgetNode(#Tree, Item)
; automatically expand the node, but keep the selection
SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected)
; give focus from the button back to the tree to show the selection
ActivateGadget(#Tree)
EndIf
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
EndIf
End
Posted: Wed Oct 26, 2005 7:38 pm
by josku_x
thanks! You are the very best! A millions of thanks, just how can I thank you? tell me, maybe your wish can come true

Posted: Thu Oct 27, 2005 2:23 am
by Sparkie
@josku_x: If I remember correctly, in your original post before you changed it, you were asking how to use a right click to select the item in TreeGadget. If you still need code for doing that, let me know.

Posted: Thu Oct 27, 2005 5:55 am
by Straker
I'd like to see that code Sparkie - please post it.
Posted: Thu Oct 27, 2005 2:07 pm
by Sparkie
Here you go Straker. It's for Windows only, tested on WinXP with PB 3.94.
Code: Select all
#TVGN_DROPHILITE = 8
Procedure WinCallback(hwnd, msg, wparam, lparam)
result = #PB_ProcessPureBasicEvents
Select msg
Case #WM_NOTIFY
*pnmhdr.NMHDR = lparam
; --> If TreeGadget receives a right click, select the item
If *pnmhdr\hwndFrom = GadgetID(0) And *pnmhdr\code = #NM_RCLICK
; --> hItem = The handle to item that has been right clicked
hItem = SendMessage_(*pnmhdr\hwndFrom, #TVM_GETNEXTITEM, #TVGN_DROPHILITE, 0)
; --> Select that item
SendMessage_(*pnmhdr\hwndFrom, #TVM_SELECTITEM, #TVGN_CARET, hItem)
EndIf
EndSelect
ProcedureReturn result
EndProcedure
If OpenWindow(0, 0, 0, 350, 200, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "Sparkies TreeGadget - Right Click Selects") And CreateGadgetList(WindowID(0))
TreeGadget(0, 10, 10, 180, 180)
For i = 0 To 5
AddGadgetItem (0, -1, "Item "+ Str(i))
AddGadgetItem (0, -1, "Parent " + Str(i))
OpenTreeGadgetNode(0)
AddGadgetItem(0, -1, "Sub Item 1")
AddGadgetItem(0, -1, "Sub Item 2")
AddGadgetItem(0, -1, "Sub Item 3")
AddGadgetItem(0, -1, "Sub Item 4")
CloseTreeGadgetNode(0)
Next
SetWindowCallback(@WinCallback())
Repeat
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
EndIf
End
Posted: Thu Oct 27, 2005 3:40 pm
by Beach
Code: Select all
; automatically expand the node, but keep the selection
SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected)
Nice... I've been looking for this!

Posted: Thu Oct 27, 2005 5:44 pm
by josku_x
Thank you everyone for your answers! now I'm just enjoying PB

Posted: Thu Oct 27, 2005 6:32 pm
by Straker
Thanks Sparkie. That helps ALOT!

TreeView - adding same level at end of child nodes
Posted: Fri Nov 11, 2005 1:36 am
by Daffodil the Duck
I know this thread is a little old but the code in it was really really helpful. Although it still leaves one issue that I just can't seem to resolve. When I am trying to add another node to my tree and I want that node to be at the same level as the node I have selected, I can do it with AddGadgetItem as per the code EXCEPT where the next row is up a level. If the following node after the selected node is up a level then the inserted node will also be up a level. I have tried a few variations and I just can't seem to solve it. Here is the sample code modified to try to demonstrate the issue:
Code: Select all
#ButtonSameLevel = 0
#ButtonChild = 1
#Tree = 2
#StringRowNumber = 3
#TextRowNumber = 4
If OpenWindow(0, 0, 0, 300, 500, #PB_Window_SystemMenu|#PB_Window_ScreenCentered, "TreeGadget")
If CreateGadgetList(WindowID())
TreeGadget(#Tree, 5, 5, 290, 400)
TextGadget(#TextRowNumber, 5, 410, 40, 20, "Row:")
StringGadget(#StringRowNumber, 35, 410, 60, 17, "", #PB_String_ReadOnly)
ButtonGadget(#ButtonSameLevel, 195, 470, 100, 25, "SameLevel")
ButtonGadget(#ButtonChild, 100, 470, 100, 25, "Child")
AddGadgetItem(#Tree, -1, "Item 0")
AddGadgetItem(#Tree, -1, "Item 1")
OpenTreeGadgetNode(#Tree, 1)
AddGadgetItem(#Tree, -1, "ChildItem 1")
AddGadgetItem(#Tree, -1, "ChildItem 2")
AddGadgetItem(#Tree, -1, "ChildItem 3")
CloseTreeGadgetNode(#Tree, 1)
AddGadgetItem(#Tree, -1, "Item 2")
AddGadgetItem(#Tree, -1, "Item 3")
AddGadgetItem(#Tree, -1, "Item 4")
Repeat
Event= WaitWindowEvent()
If Event = #PB_Event_Gadget
If EventGadgetID() = #Tree
Item = GetGadgetState(#Tree)
SetGadgetText(#StringRowNumber, Str(Item))
EndIf
If EventGadgetID() = #ButtonChild
; get the item that is selected
Item = GetGadgetState(#Tree)
; we must open the node that will be the SameLevel of our new item
OpenTreeGadgetNode(#Tree, Item)
; the new item must be below the selected one, so add at position Item+1
AddGadgetItem(#Tree, Item+1, "New Child")
; close the node again..
CloseTreeGadgetNode(#Tree, Item)
; automatically expand the node, but keep the selection
SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected)
; give focus from the button back to the tree to show the selection
ActivateGadget(#Tree)
EndIf
If EventGadgetID() = #ButtonSameLevel
; get the item that is selected
Item = GetGadgetState(#Tree)
; the new item must be below the selected one, so add at position Item+1
AddGadgetItem(#Tree, Item+1, "New SameLevel")
; automatically expand the node, but keep the selection
SetGadgetItemState(#Tree, Item, #PB_Tree_Expanded|#PB_Tree_Selected)
; give focus from the button back to the tree to show the selection
ActivateGadget(#Tree)
EndIf
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
EndIf
End
So if you run this, then select child item 2 and click 'SameLevel' and you will see a node added at the same level. Select child item 3 and click 'SameLevel' and now you see the issue I am trying to figure out. The new item is added up one level. I realise there are occasions where you might actully want this to happen but there seems no way to specify which of the two situations you want. I have figured out that if I want to add a node up a level, I can always code to go back up the tree to the last Parent node and AddGadgetItem from there. It seems to add at the same level after all the child nodes. I just can't figure it out. Help!!!