Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to drag a node from treeview?

Posted on 1998-12-30
2
Medium Priority
?
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

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

Accepted Solution

by:
Mirkwood earned 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

604 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