• We just launched and are currently in beta. Join us as we build and grow the community.

Drag/Drop and Copy Control in a TreeView Using Visual Basic 2008

Abrek

Concurrency Connoisseur
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
141
Likes
119
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, I will show you how to drag/drop and copy in a Treeview by using Visual Basic 2008. I made this to show you not only a PictureBox or a TextBox can be drag and drop but also a treeview.

Let’s begin:

Open Visual Basic 2008, create a new Windows Application and drag the two Treeview in the Form. Name it “trview_left” and “trview_right”.

firstform_4.png


After that, click the two Treeviews and go to the properties. In the properties, click the events that look like a lightning symbol and double click the Drag/Drop

event handler. And do this following code.

Declare this constant variable above the sub procedure.


  1. 'DECLARE A CONSTANT VARIABLE
  2. 'IT IS USED FOR DETECTING THE CTRL KEY WHETHER THE CTRL KEY WAS PRESSED OR NOT DURING THE DRAG OPERATION
  3. Const

    mask_ctrl As

    Int32 =

    8

  1. Private

    Sub

    TreeView_DragDrop(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    Windows

    .

    Forms

    .

    DragEventArgs

    )

    Handles

    trview_left.

    DragDrop

    , trview_right.

    DragDrop

  2. 'SET A VARIABLE TO HOLDS THE NODE TO BE DRAGGED
  3. Dim

    nodeorigination As

    TreeNode =

    CType

    (

    e.

    Data

    .

    GetData

    (

    "System.Windows.Forms.TreeNode"

    )

    , TreeNode)

  4. 'GETDATAPRESENT IS A BIT DIFFERENT FROM A TREEVIEW AND PICTUREBOX CONTROL.
  5. 'THE TREENODE IS NOT INCLUDED IN THE DATAFORMATS CLASS. THAT'S WHY IT'S NOT A PREDEFINED TYPE
  6. 'ONE OF THE GETDATAPRESENT CONTAINS A STRING THAT SPECIFY THE TYPE.
  7. If

    e.

    Data

    .

    GetDataPresent

    (

    "System.Windows.Forms.TreeNode"

    , False

    )

    Then
  8. Dim

    pnt As

    Point
  9. Dim

    distination_node As

    TreeNode

  10. 'POINTTOCLIENT IS USED TO COMPUTE THE LOCATION OF THE MOUSE OVER THE DESTINATION TREEVIEW
  11. pnt =

    CType

    (

    sender, TreeView)

    .

    PointToClient

    (

    New

    Point(

    e.

    X

    , e.

    Y

    )

    )
  12. 'POINT IS USED TO GET THE CLOSEST NODE IN THE DESTINATION TREEVIEW
  13. distination_node =

    CType

    (

    sender, TreeView)

    .

    GetNodeAt

    (

    pnt)

  14. 'IF THE NEW NODE WAS NOT DROPPED DIRECTLY AT THE TOP OF THE NODE,
  15. 'THEN THE RESULT OF THE DESTINATIONNODE WILL BE NOTHING
  16. If

    distination_node IsNot

    Nothing

    Then
  17. 'IF THE ORIGINAL NODE AND DISTINATION NODE ARE THE SAME, THE THE NODE WOULD DISAPPEAR.
  18. 'THIS CODE INSURES THAT IT WILL NOT HAPPEN.
  19. If

    Not

    distination_node.

    TreeView

    Is

    nodeorigination.

    TreeView

    Then
  20. distination_node.

    Nodes

    .

    Add

    (

    CType

    (

    nodeorigination.

    Clone

    , TreeNode)

    )
  21. 'EMPHASIZE PARENT NOT WHEN ADDING THE NEW NODE, WITHOUT THIS , ONLY THE a+ SYMBOL WILL APPEAR.
  22. distination_node.

    Expand

    (

    )

  23. If

    (

    e.

    KeyState

    And

    maskctrl)

    <>

    maskctrl Then

    'CHECKING THE CTRL KEY WAS NOT PRESSED
  24. nodeorigination.

    Remove

    (

    )

    'REMOVE THE NODE
  25. End

    If
  26. End

    If
  27. End

    If
  28. End

    If
  29. End

    Sub

Then, click the method name and choose the Drag/Enter

events handler, and do this following code.
  1. Private

    Sub

    TreeView_DragEnter(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    Windows

    .

    Forms

    .

    DragEventArgs

    )

    Handles

    trview_left.

    DragEnter

    , trview_right.

    DragEnter

  2. If

    (

    e.

    Data

    .

    GetDataPresent

    (

    "System.Windows.Forms.TreeNode"

    )

    )

    Then

    'CHECKING THE DRAG CONTENT TO BE SURE THAT IT IS THE CORRECT TYPE.

  3. If

    (

    e.

    KeyState

    And

    maskctrl)

    =

    maskctrl Then

    'CHECKINKG IF THE CTRL KEY WAS PRESSED.
  4. 'IF IT WAS PRESSED
  5. e.

    Effect

    =

    DragDropEffects.

    Copy

    'PERFORM COPY
  6. Else
  7. 'IF IT WAS NOT PRESSED
  8. e.

    Effect

    =

    DragDropEffects.

    Move

    'PERFORM A MOVE
  9. End

    If
  10. Else
  11. 'REJECT THE DROP
  12. e.

    Effect

    =

    DragDropEffects.

    None
  13. End

    If
  14. End

    Sub

Go back to the method name again and choose the Item/Drag

event handler and do this following code.
  1. Private

    Sub

    TreeView_ItemDrag(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    Windows

    .

    Forms

    .

    ItemDragEventArgs

    )

    Handles

    trview_left.

    ItemDrag

    , trview_right.

    ItemDrag
  2. If

    e.

    Button

    =

    Windows.

    Forms

    .

    MouseButtons

    .

    Left

    Then
  3. 'PERFORM THE DRAG AND DROP OPERATION.
  4. DoDragDrop(

    e.

    Item

    , DragDropEffects.

    Move

    Or

    DragDropEffects.

    Copy

    )
  5. End

    If
  6. End

    Sub

You can download the complete source code.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

327,515

327,523

Top