Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
%Up = 1 : %Down = 2
%IDC_TreeView = 100 : %IDC_ButtonUp = 200 : %IDC_ButtonDown = 201
Global hDlg As Dword
Function PBMain() As Long
Local hItem As Dword, hTemp As Dword, hTemp2 As Dword, hTemp3 As Dword
Dialog New Pixels, 0, "TreeView",200,200,155,270, %WS_SysMenu, 0 To hDlg
Control Add Treeview, hDlg, %IDC_TreeView, "", 10,10,130,200
Control Add Button, hDlg, %IDC_ButtonUp,"Move Selected Up", 10,220,130,20
Control Add Button, hDlg, %IDC_ButtonDown,"Move Selected Down", 10,245,130,20
Treeview Insert Item hDlg, %IDC_TreeView, 0, %TVI_Last, 0,0,"Root" To hItem
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Mother" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Dan" To hTemp2
Treeview Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Bob" To hTemp3
Treeview Set Expanded hDlg, %IDC_TreeView, hTemp, %True
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Father" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Helen" To hTemp2
Treeview Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Any" To hTemp3
Treeview Set Expanded hDlg, %IDC_TreeView, hTemp, %True
Treeview Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Aunt" To hTemp
Treeview Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"John" To hTemp2
Treeview Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Tommy" To hTemp3
Treeview Set Expanded hDlg, %IDC_TreeView, hTemp, %True
Treeview Set Expanded hDlg, %IDC_TreeView, hItem, %True
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If Cb.Msg = %WM_Command And Cb.Ctl = %IDC_ButtonUp Then MoveNode(%Up)
If Cb.Msg = %WM_Command And Cb.Ctl = %IDC_ButtonDown Then MoveNode(%Down)
End Function
Sub MoveNode(Flag As Long) '0 = down 1 = up
Local hNode, hNextNode, hPreviousNode, hNodeToMove, hParentNode, hRootNode As Dword, iCount As Long, temp$
'get nodes of use
Treeview Get Root hDlg, %IDC_TreeView To hRootNode
Treeview Get Select hDlg, %IDC_TreeView To hNodeToMove
Treeview Get Parent hDlg, %IDC_TreeView, hNodeToMove To hParentNode
Treeview Get Previous hDlg, %IDC_TreeView, hNodeToMove To hPreviousNode
Treeview Get Next hDlg, %IDC_TreeView, hNodeToMove To hNextNode
'conditions on which to exit
If hNodeTomove = hRootNode Then Beep : Exit Sub 'can't move root node
If hNodeToMove = 0 Then Beep : Exit Sub 'no node selected
If Flag = %Up And hPreviousNode = 0 Then Beep : Exit Sub 'no room to move up
If Flag = %Down And hNextNode = 0 Then Beep : Exit Sub 'no room to move down
'cycle through children and add an number to their name
Treeview Get Child hDlg, %IDC_TreeView, hParentNode To hNode
While hNode
Incr iCount
If hNode = hNodeToMove Then
If Flag = %Up Then 'move up
Treeview Get Text hDlg, %IDC_TreeView, hPreviousNode To temp$
Treeview Set Text hDlg, %IDC_TreeView, hPreviousNode, Format$(iCount,"00000") + Mid$(temp$,6)
Treeview Get Text hDlg, %IDC_TreeView, hNode To temp$
Treeview Set Text hDlg, %IDC_TreeView, hNode, Format$(iCount-1,"00000") + temp$
End If
If Flag = %Down Then 'move down
Treeview Get Text hDlg, %IDC_TreeView, hNextNode To temp$
Treeview Set Text hDlg, %IDC_TreeView, hNextNode, Format$(iCount,"00000") + temp$
Treeview Get Text hDlg, %IDC_TreeView, hNode To temp$
Treeview Set Text hDlg, %IDC_TreeView, hNode, Format$(iCount+1,"00000") + temp$
Incr iCount
Treeview Get Next hDlg, %IDC_TreeView, hNode To hNode 'skip next node
End If
Else
Treeview Get Text hDlg, %IDC_TreeView, hNode To temp$
Treeview Set Text hDlg, %IDC_TreeView, hNode, Format$(iCount,"00000") + temp$
End If
Treeview Get Next hDlg, %IDC_TreeView, hNode To hNode
Wend
'sort on parent node
Control Send hDlg, %IDC_TreeView, %TVM_SortChildren, %False, hParentNode 'children only
'cycle through children and remove the number
Treeview Get Child hDlg, %IDC_TreeView, hParentNode To hNode
While hNode
Treeview Get Text hDlg, %IDC_TreeView, hNode To temp$
Treeview Set Text hDlg, %IDC_TreeView, hNode, Mid$(temp$,6)
Treeview Get Next hDlg, %IDC_TreeView, hNode To hNode
Wend
End Sub
'gbs_01253
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm