none
How to properly implement node expansion within treeview OLEDragDrop event RRS feed

  • Question

  • I've implemented a treeview control within an Access .adp form populated from underlying tables.  I've been able to implement highlighting of the nodes as the user drags over the nodes and changing the mouse pointer based on pressing the Ctl key to differentiate between a copy and a move.  I've attempted, with some success, to implement the feature of having the nodes expand when the user hovers over a node for a certain length of time, but occasionally I get error 91: "Object variable or with block variable not set".  The error is coming from the timer section of the OLEDragOver event code.  The code sections for the OLEStartDrag and OLEDragOver events so far are as follows:

    Private Sub CDTree_OLEStartDrag(data as object, allowedeffects as long)
      CDTree.Object.SelectedItem = Nothing
    End Sub
    
    Private Sub CDtree_OLEDragOver(data as object, effect as long, button as integer, shift as integer, x as single, y as single, state as integer)
    Static oldnodeindx as long
    Static time0 as date
    Const Waitinterval = 1
    Dim thisnodeindx as long
    
      'set highlight
      If CDTree.SelectedItem is Nothing Then
        set CDTree.SelectedItem = CDTree.HitTest(x,y)
      End If
      CDTree.DropHighlight = CDTree.HitTest(x,y)
    
      'adjust cursor effect
      If shift and acCtrlMask then
        effect = ccOLEDropEffectCopy
      Else
        effect = ccoOLEDropEffectMove
      End If
    
      'timer to expand node
      thisnodeindx = CDTree.HitTest(x,y).Index
      If thisnodeindx <> oldnodeindx then
        timer0 = Now()
        oldnodeindx = thisnodeindx
      Else
        If DateDiff("s", time0, Now()) > Waitinterval then
          CDTree.DropHighlight.Expanded = True
        End If
      End If
    
    End Sub

    Before I dive too deeply into debugging the above code, is this the preferred/prescribed way to handle auto-expansion of nodes in a treeview control or is there some better approach?

    Thanks

    Tuesday, April 7, 2015 6:26 PM

Answers

  • After closely observing the behavior of the selecteditem and highlighted nodes, I discovered that when the mouse hovers over an area in the treeview where there is no label being displayed (blank area) the error would be thrown.  In other words, hittest is returning nothing.  So, I declared a variable "Selnode", set it to hittest, but before entering the timer block check if Selnode is nothing:

    Private Sub CDtree_OLEDragOver(data as object, effect as long, button as integer, shift as integer, x as single, y as single, state as integer)
    Static oldnodeindx as long
    Static time0 as date
    Const Waitinterval = 1
    Dim thisnodeindx as long
    Dim Selnode as Node
    
      'set highlight
      If CDTree.SelectedItem is Nothing Then
        set CDTree.SelectedItem = CDTree.HitTest(x,y)
      End If
      CDTree.DropHighlight = CDTree.HitTest(x,y)
    
      Set Selnode = CDTree.HitTest(x,y)
    
      'adjust cursor effect
      If shift and acCtrlMask then
        effect = ccOLEDropEffectCopy
      Else
        effect = ccoOLEDropEffectMove
      End If
    
      'timer to expand node
      If Not Selnode Is Nothing then
      thisnodeindx = Selnode.Index
      If thisnodeindx <> oldnodeindx then
        timer0 = Now()
        oldnodeindx = thisnodeindx
      Else
        If DateDiff("s", time0, Now()) > Waitinterval then
          CDTree.DropHighlight.Expanded = True
        End If
      End If
      End If
    
    End Sub

    • Marked as answer by macgriogar Tuesday, April 7, 2015 8:03 PM
    Tuesday, April 7, 2015 8:03 PM