473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

TreeView DragDrop

I'm writing an app that requires drag and drop operation between a ListView
and a TreeView control. (The source is the ListView). During the drag drop
operation I want to be able to detect the target node in the treeview and
auto expand it if applicable... but after a fair bit of head scratching I
can't find any easy way to accomplish this.

I think what i really need is the equivalent of the HitTest method from the
COM version of the TreeView in VB 6 ... but the DotNet version doesn't
appear to have it.

Does anyone have a code sample of how to do this ?

Thanks

Gary
Nov 21 '05 #1
3 3760
Hi Gary,

Here is some sample code that I found for doing DragDrop between 2 treeviews.

In short, I think you need the treeview.GetNodeAt(..) function to find the
target node.

///
Option Strict On
#Region "Imports Statements"
Imports System
Imports System.Drawing
Imports System.Windows.Forms
#End Region
Public Class DragDrop
Inherits Form
#Region "Entry Point"
Shared Sub Main()
Application.Run(New DragDrop())
End Sub
#End Region
#Region "Controls"
Private textboxLabel As New Label()
Private toTextBox As New TextBox()
Private fromTextBox As New TextBox()
Private treeViewLabel As New Label()
Private toTreeView As New TreeView()
Private fromTreeView As New TreeView()
Private favoriteCarsTreeNode As New TreeNode()
Private bmwTreeNode As New TreeNode()
Private porscheTreeNode As New TreeNode()
Private ferrariTreeNode As New TreeNode()
Private macLarenTreeNode As New TreeNode()
Private carsTreeNode As New TreeNode()
Private astonMartinTreeNode As New TreeNode()
Private corvetteTreeNode As New TreeNode()
Private jaguarTreeNode As New TreeNode()
Private mercedesTreeNode As New TreeNode()
Private pictureBoxLabel As New Label()
Private toPictureBox As New PictureBox()
Private fromPictureBox As New PictureBox()
Private exitButton As New Button()
#End Region
#Region "Private Fields"
Private Const ctrlPressed As Byte = 8
#End Region
#Region "Constructor"
Public Sub New()
ClientSize = New Size(430, 350)
Controls.AddRange(New Control() {textboxLabel, toTextBox, _
treeViewLabel, fromTextBox, toTreeView, fromTreeView,
pictureBoxLabel, _
toPictureBox, fromPictureBox, exitButton})
FormBorderStyle = FormBorderStyle.FixedToolWindow
Icon = New Icon(Me.GetType(), "GPS.ico")
StartPosition = FormStartPosition.CenterScreen
Text = "Drag and Drop"

With textboxLabel
.Location = New Point(10, 10)
.Size = New Size(250, 20)
.Text = "Hold Ctrl if you want to copy the text:"
End With

With toTextBox
.AllowDrop = True
.Location = New Point(10, 40)
.Size = New Size(200, 20)
.Text = "Some sample text..."
AddHandler toTextBox.MouseDown, AddressOf ToTextBoxOnMouseDown
End With

With fromTextBox
.AllowDrop = True
.Location = New Point(220, 40)
.Size = New Size(200, 20)
.Text = "This text will disappear."
AddHandler fromTextBox.DragDrop, AddressOf FromTextBoxOnDragDrop
AddHandler fromTextBox.DragEnter, AddressOf FromTextBoxOnDragEnter
End With

With treeViewLabel
.Location = New Point(10, 70)
.Size = New Size(250, 20)
.Text = "Hold Ctrl if you want to copy the tree nodes."
End With

bmwTreeNode.Text = "BMW"
porscheTreeNode.Text = "Porsche"
ferrariTreeNode.Text = "Ferrari"
macLarenTreeNode.Text = "MacLaren"

With favoriteCarsTreeNode
.Text = "Cars"
.Nodes.AddRange(New TreeNode() {bmwTreeNode, porscheTreeNode, _
ferrariTreeNode, macLarenTreeNode})
End With

astonMartinTreeNode.Text = "Aston Martin"
corvetteTreeNode.Text = "Corvette"
jaguarTreeNode.Text = "Jaguar"
mercedesTreeNode.Text = "Mercedes Benz"

With carsTreeNode
.Text = "Cars"
.Nodes.AddRange(New TreeNode() {astonMartinTreeNode,
corvetteTreeNode, _
jaguarTreeNode, mercedesTreeNode})
End With

With toTreeView
.AllowDrop = True
.Location = New Point(10, 100)
.Size = New Size(200, 100)
.Nodes.AddRange(New TreeNode() {favoriteCarsTreeNode})
AddHandler toTreeView.DragDrop, AddressOf TreeViewOnDragDrop
AddHandler toTreeView.DragEnter, AddressOf TreeViewOnDragEnter
AddHandler toTreeView.ItemDrag, AddressOf TreeViewOnItemDrag
End With

With fromTreeView
.AllowDrop = True
.Location = New Point(220, 100)
.Size = New Size(200, 100)
.Nodes.AddRange(New TreeNode() {carsTreeNode})
AddHandler fromTreeView.DragDrop, AddressOf TreeViewOnDragDrop
AddHandler fromTreeView.DragEnter, AddressOf TreeViewOnDragEnter
AddHandler fromTreeView.ItemDrag, AddressOf TreeViewOnItemDrag
End With

With pictureBoxLabel
.Location = New Point(10, 220)
.Size = New Size(250, 20)
.Text = "Hold Ctrl if you want to copy the icon."
End With

With toPictureBox
.AllowDrop = True
.BorderStyle = BorderStyle.Fixed3D
.Image = New Bitmap(Me.GetType(), "GPS.ico")
.Location = New Point(80, 250)
.Name = "toPictureBox"
.Size = New Size(50, 50)
.SizeMode = PictureBoxSizeMode.CenterImage
AddHandler toPictureBox.MouseDown, AddressOf PictureBoxOnMouseDown
AddHandler toPictureBox.DragEnter, AddressOf PictureBoxOnDragEnter
AddHandler toPictureBox.DragDrop, AddressOf PictureBoxOnDragDrop
End With

With fromPictureBox
.AllowDrop = True
.BorderStyle = BorderStyle.Fixed3D
.Location = New Point(290, 250)
.Name = "fromPictureBox"
.Size = New Size(50, 50)
.SizeMode = PictureBoxSizeMode.CenterImage
AddHandler fromPictureBox.MouseDown, AddressOf
PictureBoxOnMouseDown
AddHandler fromPictureBox.DragEnter, AddressOf
PictureBoxOnDragEnter
AddHandler fromPictureBox.DragDrop, AddressOf PictureBoxOnDragDrop
End With

With exitButton
.FlatStyle = FlatStyle.System
.Location = New Point(350, 310)
.Size = New Size(70, 30)
.Text = "E&xit"
AddHandler exitButton.Click, AddressOf ExitOnClick
End With
End Sub
#End Region
#Region "Methods"
Private Sub ToTextBoxOnMouseDown(ByVal obj As Object, _
ByVal mea As MouseEventArgs)
If mea.Button = MouseButtons.Left Then
toTextBox.SelectAll()
toTextBox.DoDragDrop(toTextBox.SelectedText,
DragDropEffects.Move Or _
DragDropEffects.Copy)
End If
End Sub
Private Sub FromTextBoxOnDragDrop(ByVal obj As Object, _
ByVal dea As DragEventArgs)
fromTextBox.Text = dea.Data.GetData(DataFormats.Text).ToString()
If (dea.KeyState And ctrlPressed) <> ctrlPressed Then
toTextBox.Text = ""
End If
End Sub
Private Sub FromTextBoxOnDragEnter(ByVal obj As Object, _
ByVal dea As DragEventArgs)
If (dea.Data.GetDataPresent(DataFormats.Text)) Then
If (dea.KeyState And ctrlPressed) = ctrlPressed Then
dea.Effect = DragDropEffects.Copy
Else
dea.Effect = DragDropEffects.Move
End If
Else
dea.Effect = DragDropEffects.None
End If
End Sub
Private Sub TreeViewOnDragDrop(ByVal obj As Object, _
ByVal dea As DragEventArgs)
Dim draggedNode As TreeNode = _
CType(dea.Data.GetData("System.Windows.Forms.TreeN ode"), TreeNode)
If dea.Data.GetDataPresent("System.Windows.Forms.Tree Node", False)
Then
Dim drawingPoint As Point
Dim destinationNode As TreeNode
drawingPoint = CType(obj, TreeView).PointToClient _
(New Point(dea.X, dea.Y))
destinationNode = CType(obj, TreeView).GetNodeAt(drawingPoint)
If Not destinationNode.TreeView Is draggedNode.TreeView Then
destinationNode.Nodes.Add(CType(draggedNode.Clone, TreeNode))
destinationNode.Expand()
If (dea.KeyState And ctrlPressed) <> ctrlPressed Then
draggedNode.Remove()
End If
End If
End If
End Sub
Private Sub TreeViewOnDragEnter(ByVal obj As Object, _
ByVal dea As DragEventArgs)
If (dea.Data.GetDataPresent("System.Windows.Forms.Tre eNode")) Then
If (dea.KeyState And ctrlPressed) = ctrlPressed Then
dea.Effect = DragDropEffects.Copy
Else
dea.Effect = DragDropEffects.Move
End If
Else
dea.Effect = DragDropEffects.None
End If
End Sub
Private Sub TreeViewOnItemDrag(ByVal obj As Object, _
ByVal idea As ItemDragEventArgs)
If idea.Button = MouseButtons.Left Then
DoDragDrop(idea.Item, DragDropEffects.Move Or
DragDropEffects.Copy)
End If
End Sub
Private Sub PictureBoxOnMouseDown(ByVal obj As Object, _
ByVal mea As MouseEventArgs)
If mea.Button = MouseButtons.Left Then
Dim image As PictureBox = CType(obj, PictureBox)
If Not image.Image Is Nothing Then
image.DoDragDrop(image.Image, DragDropEffects.Move Or _
DragDropEffects.Copy)
End If
End If
End Sub
Private Sub PictureBoxOnDragEnter(ByVal obj As Object, _
ByVal dea As DragEventArgs)
If (dea.Data.GetDataPresent(DataFormats.Bitmap)) Then
If (dea.KeyState And ctrlPressed) = ctrlPressed Then
dea.Effect = DragDropEffects.Copy
Else
dea.Effect = DragDropEffects.Move
End If
Else
dea.Effect = DragDropEffects.None
End If
End Sub
Private Sub PictureBoxOnDragDrop(ByVal obj As Object, _
ByVal dea As DragEventArgs)
Dim picture As PictureBox = CType(obj, PictureBox)
picture.Image = CType(dea.Data.GetData(DataFormats.Bitmap), Bitmap)
If (dea.KeyState And ctrlPressed) <> ctrlPressed Then
If picture.Name = "toPictureBox" Then
fromPictureBox.Image = Nothing
Else
toPictureBox.Image = Nothing
End If
End If
End Sub
Private Sub ExitOnClick(ByVal obj As Object, ByVal ea As EventArgs)
Application.Exit()
End Sub
#End Region
End Class
\\\

HTH
Nov 21 '05 #2

in tree dragover event:


Dim mousePos As Point

mousePos = TreeView.PointToClient(Cursor.Position)

Dim nodeOver As TreeViewNode = TreeView.GetNodeAt(mousePos)

If nodeOver Is Nothing Then
.... we aren't over a node.
Else
.... we are over a node, so determine whether we are allowed to
drag/drop
End If


"Gary Dunne" <Mo*********@Eircom.Net> wrote in message
news:O5*************@TK2MSFTNGP15.phx.gbl...
I'm writing an app that requires drag and drop operation between a
ListView
and a TreeView control. (The source is the ListView). During the drag drop
operation I want to be able to detect the target node in the treeview and
auto expand it if applicable... but after a fair bit of head scratching I
can't find any easy way to accomplish this.

I think what i really need is the equivalent of the HitTest method from
the
COM version of the TreeView in VB 6 ... but the DotNet version doesn't
appear to have it.

Does anyone have a code sample of how to do this ?

Thanks

Gary

Nov 21 '05 #3
Thanks for the help..... it works nicely
"Gary Dunne" <Mo*********@Eircom.Net> wrote in message
news:O5*************@TK2MSFTNGP15.phx.gbl...
I'm writing an app that requires drag and drop operation between a ListView and a TreeView control. (The source is the ListView). During the drag drop
operation I want to be able to detect the target node in the treeview and
auto expand it if applicable... but after a fair bit of head scratching I
can't find any easy way to accomplish this.

I think what i really need is the equivalent of the HitTest method from the COM version of the TreeView in VB 6 ... but the DotNet version doesn't
appear to have it.

Does anyone have a code sample of how to do this ?

Thanks

Gary

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Plumer | last post by:
Hello everyone, I am using MS .NET Framework 1.0 Version 1.0.3705 developing a C# client application using SQL Server on the server side. I am looking to implement drag & drop in a TreeView...
1
by: '[] WiRaN | last post by:
anywhere user drag and drop in treeview??? please, send me example... Wiran
1
by: SteveK | last post by:
I want to make a little utility that will allow me to drag a folder onto a treeView and then poppulate the treeView with the contents of the dragged folder. So far things aren't going well. I...
1
by: K Hayes | last post by:
Hi All, Newish VB.NET programmer seeking help! I have a TreeView with a node that I wish to drag and drop to a Textbox. In the Treeview_ItemDrag i have
6
by: L.M | last post by:
Hello, I knew how to use the treeview under VB6. After migrating to .NET, well, I'm lost. I try to add a new node, either to the same level or as a child to a selected node in the treeview....
3
by: Kelvin Leung | last post by:
Hi I use Drag and Drop between 2 TreeView Control under VB.Net But I found that it cannot work when I add sub-class for each node Is it drag and drop method cannot work when the node with...
1
by: pooja | last post by:
i need to implement drag and drop in treeview in VB. Kindly help. My treeview contains activities maintained using XML Files. Hopefully, Thanks.
1
by: dotnetnari | last post by:
hi, How to Drag and drop the nodes from treeview into datatable columns using windowsapplication in c# Thanks
3
by: dutsnekcirf | last post by:
I have a treeview control on a custom task pane in Excel. I've enable the ability to use Drag & Drop (by following this how-to) on the treeview to change the order of the nodes. The problem though...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.