Announcement

Collapse
No announcement yet.

TREEVIEW Drag&Drop

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    Thanks Gary for the code. VERY much appreciated.

    Comment


    • #22
      bummer. i just now saw the indenting problem mentioned in the other thread. will try to fix.

      Comment


      • #23
        HTML Code:
        [code]'Compilable Example:
        #Compile Exe
        #Dim All
        %Unicode=1
        #Include "Win32API.inc"
        %IDC_TreeView    = 500
        %IDC_TextBoxIn   = 501
        %IDC_TextBoxOut  = 502
        %IDC_ButtonBuild = 503
        %IDC_ButtonGet   = 504
        Global hDlg As Dword
        
        Function PBMain() As Long
           Dialog New Pixels, 0, "TreeView",200,200,590,250, %WS_SysMenu, 0 To hDlg
           Control Add Button, hDlg, %IDC_ButtonBuild, "Build TreeView", 10,5,125,25
           Control Add Button, hDlg, %IDC_ButtonGet, "Build Text From TreeView", 400,5,150,25
           Control Add TextBox, hDlg, %IDC_TextBoxIn,"", 10,40,180,200, %ES_MultiLine Or %ES_WantReturn
           Control Add Treeview, hDlg, %IDC_TreeView,"", 200,40,190,200
           Control Add TextBox, hDlg, %IDC_TextBoxOut,"", 400,40,180,200, %ES_MultiLine Or %ES_WantReturn
           Dialog Show Modal hDlg Call DlgProc
        End Function
        
        CallBack Function DlgProc() As Long
           Local temp$, hNode As Dword
           Select Case Cb.Msg
              Case %WM_InitDialog
                 Control Set Text hDlg, %IDC_TextBoxIn, _
                 "City" + $CrLf + _
                 "...Dallas" + $CrLf + _
                 "...Chicago" + $CrLf + _
                 "...New York" + $CrLf + _
                 "State" + $CrLf + _
                 "...Texas" + $CrLf + _
                 "......Cowboys" + $CrLf + _
                 "......Cowgirls" + $CrLf + _
                 "...New York" + $CrLf + _
                 "Idaho"
              Case %WM_Command
                 Select Case Cb.Ctl
                    Case %IDC_ButtonBuild
                       Control Get Text hDlg, %IDC_TextBoxIn To temp$
                       BuildTreeViewCode(temp$,1,1)
                    Case %IDC_ButtonGet
                       Treeview Get Select hDlg, %IDC_TreeView To hNode
                       Control Set Text hDlg, %IDC_TextBoxOut, GetTreeViewContent(hNode)
                 End Select
           End Select
        End Function
        
        Function BuildTreeViewCode(TVContent As String, ShowRoot As Long, ExpandAll As Long) As String
           Local record$, caption$, recordnext$, hItem, hTemp As Dword, i,DepthCurrent, DepthNext As Long
           ReDim hParent(100) As Dword, Parent(100) As String
           'add root node if required
           If ShowRoot Then Treeview Insert Item  hDlg, %IDC_TreeView, 0, %TVI_Last, 0, 0, "Root" To hParent(0)
           'build interior
           For i = 1 To ParseCount(TVcontent,$CrLf)
              'Get info about Current and Next records
              record$      = Parse$(TVcontent,$CrLf,i)
              depthcurrent = ParseCount(record$,"...")
              caption$     = LTrim$(record$,".")
              recordnext$  = Parse$(TVContent,$CrLf,i+1)
              depthnext    = ParseCount(recordnext$,"...")
              'put current record into TreeView
              If DepthNext > DepthCurrent Then 'is parent node
                 Treeview Insert Item hDlg, %IDC_TreeView, hParent(depthcurrent-1), %TVI_Last, 0,0,caption$ To hItem  'add parent
                 hParent(DepthCurrent) = hItem  'save parent node value
              Else 'is child node
                 Treeview Insert Item hDlg, %IDC_TreeView, hParent(depthcurrent-1), %TVI_Last, 0,0,caption$ To hTemp  'add child
                 If ExpandAll Then Treeview Set Expanded hDlg, %IDC_TreeView, hParent(depthcurrent-1), 1   'expand parent
              End If
           Next i
        End Function
        
        Function GetTreeViewContent(hNode As Dword) As String
           Local iReturn As Dword, temp$,tmp$, D As Long
           'Treeview Get Root hDlg, %IDC_TreeView To hNode
           Do
              Treeview Get Child hDlg, %IDC_TreeView, hNode To iReturn                      'get child (1st choice)
              If iReturn = 0 Then Treeview Get Next hDlg, %IDC_TreeView, hNode To iReturn Else Incr D   'or sibling (2nd choice)
              If iReturn = 0 Then                                                        'no child or sibling
                 Do                                                                      'get sibling of first parent with sibling
                    Treeview Get Parent hDlg, %IDC_TreeView, hNode To hNode              'parent
                    If hNode Then Decr D
                    Treeview Get Next hDlg, %IDC_TreeView, hNode To iReturn              'sibling child of parent
                 Loop Until iReturn Or (hNode = 0)  'stop when find sibling of parent with sibling, or no more choices
              End If
              hNode = iReturn    'possible values: 0, zero (no parent/no sibling), <>0 (parent or sibling)
              Treeview Get Text hDlg, %IDC_TreeView, hNode To tmp$ : temp$ = temp$ + $CrLf + Repeat$(D-1,"...") + tmp$
           Loop While hNode
           Function = Trim$(temp$,$CrLf)
        End Function
        
        
        'gbs_01259
        'Date: 05-11-2013[/code]

        Comment


        • #24
          Ok, Steve, give this is a try. It provides Cut, PasteAsSibling and PasteAsChild options.

          When pasting, the pasted content becomes the last child under the parent.

          HTML Code:
          [code]'Compilable Example:
          #Compiler PBWin 9, PBWin 10
          #Compile Exe
          #Dim All
          %Unicode=1
          #Include "Win32API.inc"
          %IDC_TreeView     = 100
          %IDC_Cut          = 200
          %IDC_Copy         = 250
          %IDC_PasteChild   = 300
          %IDC_PasteSibling = 400
          %IDC_PasteContent = 500
          %IDC_Reset        = 600
          Global hDlg As Dword, PasteContent$
          
          Function PBMain() As Long
             Local BoxStyle As Long
             BoxStyle = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
                      Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
             Dialog Default Font "Tahoma", 12, 1
             Dialog New Pixels, 0, "TreeView",200,200,385,590, %WS_SysMenu, 0 To hDlg
             Control Add TextBox, hDlg, %IDC_PasteContent, "", 200,10,180,430, BoxStyle, %WS_Ex_ClientEdge
             Control Add Button, hDlg, %IDC_Reset,"Reset", 200,450,110,25
             Control Add Button, hDlg, %IDC_Copy,"Copy", 25,450,110,25
             Control Add Button, hDlg, %IDC_Cut,"Cut", 25,480,110,25
             Control Add Button, hDlg, %IDC_PasteSibling,"Paste_Sibling", 25,510,110,25
             Control Add Button, hDlg, %IDC_PasteChild,"Paste_Child", 25,540,110,25
             ResetTreeView
             Dialog Show Modal hDlg Call DlgProc
          End Function
          
          CallBack Function DlgProc() As Long
             Local hNode As Dword, temp$
             Select Case Cb.Msg
                Case %WM_Command
                   Select Case Cb.Ctl
                      Case %IDC_Reset
                         ResetTreeView
                      Case %IDC_Cut
                         Treeview Get Select Cb.Hndl, %IDC_TreeView To hNode
                         If hNode = 0 Then ? "No slection!" : Exit Function
                         WalkThroughNode (hNode)
                         Control Set Text hDlg, %IDC_PasteContent, PasteContent$
                         Treeview Delete hDlg, %IDC_TreeView, hNode
                      Case %IDC_Copy
                         Treeview Get Select Cb.Hndl, %IDC_TreeView To hNode
                         If hNode = 0 Then ? "No slection!" : Exit Function
                         WalkThroughNode (hNode)
                         Control Set Text hDlg, %IDC_PasteContent, PasteContent$
                      Case %IDC_PasteSibling
                         Clipboard Get Text To temp$
                         If Len(temp$) = 0 Then ? "Clipboard Empty!" : Exit Function
                         PasteIntoTreeView 1
                      Case %IDC_PasteChild
                         Clipboard Get Text To temp$
                         If Len(temp$) = 0 Then ? "Clipboard Empty!" : Exit Function
                         PasteIntoTreeView 0
                   End Select
             End Select
          End Function
          
          Sub ResetTreeView
             Local hItem As Dword, hTemp As Dword, hTemp2 As Dword, hTemp3 As Dword
             Control Kill hDlg, %IDC_TreeView
             Control Add Treeview, hDlg, %IDC_TreeView, "", 10,10,180,430
             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, hTemp3, %TVI_Last, 0,0,"Foot" To hTemp2
             Treeview Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Arm" To hTemp2
             Treeview Set Expanded hDlg, %IDC_TreeView, hTemp3, %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, hTemp2, %TVI_Last, 0,0,"Left" To hTemp3
             Treeview Insert Item hDlg, %IDC_TreeView, hTemp2, %TVI_Last, 0,0,"Right" To hTemp3
             Treeview Set Expanded hDlg, %IDC_TreeView, hTemp2, %True
          
             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, hTemp3, %TVI_Last, 0,0,"Leg" To hTemp2
             Treeview Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Finger" To hTemp2
             Treeview Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
             Treeview Set Expanded hDlg, %IDC_TreeView, hItem, %True
          End Sub
          
          Function WalkThroughNode(ByVal hNode As Dword) As Dword
             'walks through (selects) children+subnodes of specified Node
             'returns last node
             'returns 0 if no children or subnodes
             Local iReturn As Dword, hStartNode As Dword, D As Long, tmp$
             hStartNode = hNode
             Treeview Get Text hDlg, %IDC_TreeView, hStartNode To PasteContent$
             PasteContent$ = "001 " + PasteContent$
             D = 1
             Do
                Function = hNode
                Treeview Get Child hDlg, %IDC_TreeView, hNode To iReturn                      'get child (1st choice)
                If iReturn = 0 Then Treeview Get Next hDlg, %IDC_TreeView, hNode To iReturn Else Incr D   'or sibling (2nd choice)
                If iReturn = 0 Then                                                        'no child or sibling
                   Do                                                                      'get sibling of first parent with sibling
                      Treeview Get Parent hDlg, %IDC_TreeView, hNode To hNode              'parent
                      If hNode Then Decr D
                      If hNode = hStartNode Then iReturn = 0 : Exit Loop                   'if reach starting node, stop
                      Treeview Get Next hDlg, %IDC_TreeView, hNode To iReturn              'sibling child of parent
                   Loop Until iReturn Or (hNode = 0)  'stop when find sibling of parent with sibling, or no more choices
                End If
                hNode = iReturn    'possible values: 0, zero (no parent/no sibling), <>0 (parent or sibling)
                'do something here-----------
                If hNode And D > 0 Then Treeview Get Text hDlg, %IDC_TreeView, hNode To tmp$ : PasteContent$ += $CrLf + Format$(D,"000") + " " + tmp$
                'If hNode and D > 0 Then TreeView Select hDlg, %IDC_TreeView, hNode : Sleep 300 'for demo only - selection/pause is optional
             Loop While hNode And D > 0
          End Function
          
          Function PasteIntoTreeView(Sibling As Long) As String   'PasteContent$ is a Global string variable
             Local record$, caption$, recordnext$, hItem, hTemp,h As Dword, i,DepthCurrent, DepthNext As Long
             ReDim hParent(100) As Dword, Parent(100) As String
          
             'determine parent of initial position
             Treeview Get Select hDlg, %IDC_TreeView To hItem
             If Sibling = 1 Then
                Treeview Get Parent hDlg, %IDC_TreeView, hItem To hParent(0)  'sibling
             Else
                hParent(0) = hItem                                            'child
             End If
          
             'build interior
             Treeview Get Select hDlg, %IDC_TreeView To hItem
             For i = 1 To ParseCount(Pastecontent,$CrLf)
                'Get info about Current and Next records
                record$      = Parse$(Pastecontent,$CrLf,i)
                depthcurrent = Val(record$)
                caption$     = Parse$(record$,$Spc,2)
                recordnext$  = Parse$(PasteContent,$CrLf,i+1)
                depthnext    = Val(recordnext$)
                'put current record into TreeView
                If DepthNext > DepthCurrent Then 'is parent node
                   Treeview Insert Item hDlg, %IDC_TreeView, hParent(depthcurrent-1), %TVI_Last, 0,0,caption$ To hItem  'add parent
                   hParent(DepthCurrent) = hItem  'save parent node value
                Else 'is child node
                   Treeview Insert Item hDlg, %IDC_TreeView, hParent(depthcurrent-1), %TVI_Last, 0,0,caption$ To hTemp  'add child
                End If
                Treeview Set Expanded hDlg, %IDC_TreeView, hParent(depthcurrent-1), 1                                'expand parent
             Next i
          End Function
          [/code]
          Last edited by Gary Beene; 19 Jan 2020, 12:35 PM.

          Comment


          • #25
            Added:
            1. Copy only
            2. Detects whether a selection has been made
            3. Detects empty clipboard.

            Comment


            • #26
              Nice work Gary, very useful

              Comment

              Working...
              X