How to drag a node from treeview?

Posted on 1998-12-30
Last Modified: 2010-05-03
I've made a treeview with VB5.0,but I don't know how to drag
¡¢Copy and Paste it.
Question by:joanf

Author Comment

ID: 1453158
Edited text of question
LVL 13

Accepted Solution

Mirkwood earned 100 total points
ID: 1453159
Here is a good sample

HOWTO: Scroll the TreeView When Dragging and Dropping a Node
Last reviewed: December 8, 1997
Article ID: Q177743  
The information in this article applies to:
Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 5.0

The TreeView control supports drag-and-drop. When a Node is dragged to the top or bottom of the control, the control does not automatically scroll vertically. Because the control does not support scrolling when doing drag- and-drop, scrolling must be implemented by the developer. This article demonstrates one way to implement scrolling when performing drag-and-drop.

The following example uses API calls to scroll the TreeView when the mouse pointer gets close to the bottom or top of the control and you are doing a drag-and-drop of a Node.

Step-by-Step Example

Create a new standard EXE project.Form1 is created by default.

Click Components on the Project menu. Check "Microsoft Windows Common Controls 5.0."

Add a Treeview, ImageList, and Timer to Form1.

Right-click the ImageList Control and select Properties from the pop-up menu.

On the General Tab of the Property Pages dialog, select 16 x 16.

Click the Images Tab, and insert 2 ICO files. NOTE: Icon files can be found in the directory ..\VB\Graphics\Icon.

Add the following code to the Code Window of Form1:

      Option Explicit

      Private Declare Function SendMessage Lib "user32" Alias _
        "SendMessageA" (ByVal hwnd As Long, _
        ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
      Dim mfX As Single
      Dim mfY As Single
      Dim moNode As Node
      Dim m_iScrollDir As Integer 'Which way to scroll
      Dim mbFlag As Boolean

      Private Sub Form_DragOver(Source As Control, x As Single, y As _
        Single, State As Integer)
         If Source.Name = "TreeView1" Then
            Timer1.Enabled = False
         End If
      End Sub

      Private Sub Form_Load()
         Dim i As Integer
         Dim n As Integer
         Timer1.Enabled = False
         Timer1.Interval = 200
         TreeView1.Style = tvwTreelinesPlusMinusPictureText
         TreeView1.ImageList = ImageList1
         'Populate TreeView controls
         For i = 1 To 50
           TreeView1.Nodes.Add Text:="Node " & i, Image:=1, _
         Next i
         For i = 1 To 50
           For n = 1 To 5
              TreeView1.Nodes.Add Relative:=i, Relationship:=tvwChild, _
               Text:="Child Node " & n, _
               Image:=1, SelectedImage:=2
           Next n
         Next i
      End Sub

      Private Sub Timer1_Timer()
         Set TreeView1.DropHighlight = TreeView1.HitTest(mfX, mfY)
         If m_iScrollDir = -1 Then 'Scroll Up
         ' Send a WM_VSCROLL message 0 is up and 1 is down
           SendMessage TreeView1.hwnd, 277&, 0&, vbNull
         Else 'Scroll Down
           SendMessage TreeView1.hwnd, 277&, 1&, vbNull
         End If
      End Sub

      Private Sub TreeView1_DragDrop(Source As Control, x As Single, _
        y As Single)
      ' If you were moving the Node you dragged to be the child
      ' of the Node you dropped it on, you would need to
      ' make moNode the selected Node after you made it a child
        If Not TreeView1.DropHighlight Is Nothing Then
          MsgBox moNode.Text & " was dropped on " & _
        End If
        Set TreeView1.DropHighlight = Nothing
        Set moNode = Nothing
        Timer1.Enabled = False
      End Sub

      Private Sub TreeView1_DragOver(Source As Control, x As Single, _
      y As Single, State As Integer)
        Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
        mfX = x
        mfY = y
        If y > 0 And y < 100 Then 'scroll up
          m_iScrollDir = -1
          Timer1.Enabled = True
        ElseIf y > (TreeView1.Height - 200) And y < TreeView1.Height Then
        'scroll down
          m_iScrollDir = 1
          Timer1.Enabled = True
          Timer1.Enabled = False
        End If
      End Sub

      Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, _
      x As Single, y As Single)
       Timer1.Enabled = False
      End Sub

      Private Sub TreeView1_MouseDown(Button As Integer, Shift As _
      Integer, x As Single, y As Single)
       TreeView1.DropHighlight = TreeView1.HitTest(x, y)
       'Make sure we are over a Node
       If Not TreeView1.DropHighlight Is Nothing Then
          TreeView1.SelectedItem = TreeView1.HitTest(x, y)
          Set moNode = TreeView1.SelectedItem ' Set the item being dragged.
       End If
       Set TreeView1.DropHighlight = Nothing
      End Sub

      Private Sub TreeView1_MouseMove(Button As Integer, Shift As _
      Integer, x As Single, y As Single)
       If Button = vbLeftButton Then
          TreeView1.DragIcon = TreeView1.SelectedItem.CreateDragImage
          TreeView1.Drag vbBeginDrag
       End If
      End Sub

Save and run the sample. Select a Node and then drag it to the bottom of the TreeView. You will see the TreeView scroll.

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q172272
   TITLE     : SAMPLE: Manipulating and Saving the Nodes in a TreeView

For more information on the TreeView Control, please see the Visual Basic Online Help.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question