none
tree view autoscroll when drag drop an Item

    Question

  • Hi,

     

    I was really in need of your help. I have developed an application In which I'm doing drag drop an item on to a tree view from list box. when I was dragging the Item on to it the tree view is expanding and is scrolling down but when i want to drop the item on the top nodes which are not visible, I was not able to do that. tree is not scrolling up. I have tried every option i knew. please help me in this. thanks in advance.

     

    -regards

    GRK

    Monday, June 25, 2007 7:08 AM

Answers

  • Try this:

    Private Sub TreeView1_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragOver
        Dim pos As Point = TreeView1.PointToClient(New Point(e.X, e.Y))
        Dim node As TreeNode = TreeView1.GetNodeAt(pos)
        If node Is Nothing Then
          e.Effect = DragDropEffects.None
        Else
          e.Effect = DragDropEffects.Copy
          node.Expand()
          If pos.Y <= TreeView1.Font.Height \ 2 Then
            '--- Getting close to top, ensure previous node is visible
            node = node.PrevVisibleNode
          ElseIf pos.Y >= TreeView1.ClientSize.Height - TreeView1.Font.Height \ 2 Then
            '--- Getting close to bottom, ensure next node is visible
            node = node.NextVisibleNode
          Else
            node = Nothing
          End If
          If node IsNot Nothing And Not Timer1.Enabled Then
            node.EnsureVisible()
            Timer1.Enabled = True
          End If
        End If
      End Sub
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Enabled = False
      End Sub

    The timer is necessary to slow down the scrolling.  Set its Interval to 250 msec or so.
    Monday, June 25, 2007 8:50 AM