Solved

How to drag a node from treeview?

Posted on 1998-12-30
2
283 Views
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.
0
Comment
Question by:joanf
2 Comments
 

Author Comment

by:joanf
ID: 1453158
Edited text of question
0
 
LVL 13

Accepted Solution

by:
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


SUMMARY
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.



MORE INFORMATION
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, _
              SelectedImage:=2
         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 " & _
          TreeView1.DropHighlight.Text
        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
        Else
          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.

REFERENCES
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
               Control

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

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now