473,578 Members | 3,125 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3778
Hi Gary,

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

In short, I think you need the treeview.GetNod eAt(..) 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 favoriteCarsTre eNode As New TreeNode()
Private bmwTreeNode As New TreeNode()
Private porscheTreeNode As New TreeNode()
Private ferrariTreeNode As New TreeNode()
Private macLarenTreeNod e As New TreeNode()
Private carsTreeNode As New TreeNode()
Private astonMartinTree Node As New TreeNode()
Private corvetteTreeNod e As New TreeNode()
Private jaguarTreeNode As New TreeNode()
Private mercedesTreeNod e 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 "Constructo r"
Public Sub New()
ClientSize = New Size(430, 350)
Controls.AddRan ge(New Control() {textboxLabel, toTextBox, _
treeViewLabel, fromTextBox, toTreeView, fromTreeView,
pictureBoxLabel , _
toPictureBox, fromPictureBox, exitButton})
FormBorderStyle = FormBorderStyle .FixedToolWindo w
Icon = New Icon(Me.GetType (), "GPS.ico")
StartPosition = FormStartPositi on.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.Mouse Down, AddressOf ToTextBoxOnMous eDown
End With

With fromTextBox
.AllowDrop = True
.Location = New Point(220, 40)
.Size = New Size(200, 20)
.Text = "This text will disappear."
AddHandler fromTextBox.Dra gDrop, AddressOf FromTextBoxOnDr agDrop
AddHandler fromTextBox.Dra gEnter, AddressOf FromTextBoxOnDr agEnter
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.Tex t = "BMW"
porscheTreeNode .Text = "Porsche"
ferrariTreeNode .Text = "Ferrari"
macLarenTreeNod e.Text = "MacLaren"

With favoriteCarsTre eNode
.Text = "Cars"
.Nodes.AddRange (New TreeNode() {bmwTreeNode, porscheTreeNode , _
ferrariTreeNode , macLarenTreeNod e})
End With

astonMartinTree Node.Text = "Aston Martin"
corvetteTreeNod e.Text = "Corvette"
jaguarTreeNode. Text = "Jaguar"
mercedesTreeNod e.Text = "Mercedes Benz"

With carsTreeNode
.Text = "Cars"
.Nodes.AddRange (New TreeNode() {astonMartinTre eNode,
corvetteTreeNod e, _
jaguarTreeNode, mercedesTreeNod e})
End With

With toTreeView
.AllowDrop = True
.Location = New Point(10, 100)
.Size = New Size(200, 100)
.Nodes.AddRange (New TreeNode() {favoriteCarsTr eeNode})
AddHandler toTreeView.Drag Drop, AddressOf TreeViewOnDragD rop
AddHandler toTreeView.Drag Enter, AddressOf TreeViewOnDragE nter
AddHandler toTreeView.Item Drag, AddressOf TreeViewOnItemD rag
End With

With fromTreeView
.AllowDrop = True
.Location = New Point(220, 100)
.Size = New Size(200, 100)
.Nodes.AddRange (New TreeNode() {carsTreeNode})
AddHandler fromTreeView.Dr agDrop, AddressOf TreeViewOnDragD rop
AddHandler fromTreeView.Dr agEnter, AddressOf TreeViewOnDragE nter
AddHandler fromTreeView.It emDrag, AddressOf TreeViewOnItemD rag
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.Fix ed3D
.Image = New Bitmap(Me.GetTy pe(), "GPS.ico")
.Location = New Point(80, 250)
.Name = "toPictureB ox"
.Size = New Size(50, 50)
.SizeMode = PictureBoxSizeM ode.CenterImage
AddHandler toPictureBox.Mo useDown, AddressOf PictureBoxOnMou seDown
AddHandler toPictureBox.Dr agEnter, AddressOf PictureBoxOnDra gEnter
AddHandler toPictureBox.Dr agDrop, AddressOf PictureBoxOnDra gDrop
End With

With fromPictureBox
.AllowDrop = True
.BorderStyle = BorderStyle.Fix ed3D
.Location = New Point(290, 250)
.Name = "fromPictureBox "
.Size = New Size(50, 50)
.SizeMode = PictureBoxSizeM ode.CenterImage
AddHandler fromPictureBox. MouseDown, AddressOf
PictureBoxOnMou seDown
AddHandler fromPictureBox. DragEnter, AddressOf
PictureBoxOnDra gEnter
AddHandler fromPictureBox. DragDrop, AddressOf PictureBoxOnDra gDrop
End With

With exitButton
.FlatStyle = FlatStyle.Syste m
.Location = New Point(350, 310)
.Size = New Size(70, 30)
.Text = "E&xit"
AddHandler exitButton.Clic k, AddressOf ExitOnClick
End With
End Sub
#End Region
#Region "Methods"
Private Sub ToTextBoxOnMous eDown(ByVal obj As Object, _
ByVal mea As MouseEventArgs)
If mea.Button = MouseButtons.Le ft Then
toTextBox.Selec tAll()
toTextBox.DoDra gDrop(toTextBox .SelectedText,
DragDropEffects .Move Or _
DragDropEffects .Copy)
End If
End Sub
Private Sub FromTextBoxOnDr agDrop(ByVal obj As Object, _
ByVal dea As DragEventArgs)
fromTextBox.Tex t = dea.Data.GetDat a(DataFormats.T ext).ToString()
If (dea.KeyState And ctrlPressed) <> ctrlPressed Then
toTextBox.Text = ""
End If
End Sub
Private Sub FromTextBoxOnDr agEnter(ByVal obj As Object, _
ByVal dea As DragEventArgs)
If (dea.Data.GetDa taPresent(DataF ormats.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 TreeViewOnDragD rop(ByVal obj As Object, _
ByVal dea As DragEventArgs)
Dim draggedNode As TreeNode = _
CType(dea.Data. GetData("System .Windows.Forms. TreeNode"), TreeNode)
If dea.Data.GetDat aPresent("Syste m.Windows.Forms .TreeNode", False)
Then
Dim drawingPoint As Point
Dim destinationNode As TreeNode
drawingPoint = CType(obj, TreeView).Point ToClient _
(New Point(dea.X, dea.Y))
destinationNode = CType(obj, TreeView).GetNo deAt(drawingPoi nt)
If Not destinationNode .TreeView Is draggedNode.Tre eView Then
destinationNode .Nodes.Add(CTyp e(draggedNode.C lone, TreeNode))
destinationNode .Expand()
If (dea.KeyState And ctrlPressed) <> ctrlPressed Then
draggedNode.Rem ove()
End If
End If
End If
End Sub
Private Sub TreeViewOnDragE nter(ByVal obj As Object, _
ByVal dea As DragEventArgs)
If (dea.Data.GetDa taPresent("Syst em.Windows.Form s.TreeNode")) 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 TreeViewOnItemD rag(ByVal obj As Object, _
ByVal idea As ItemDragEventAr gs)
If idea.Button = MouseButtons.Le ft Then
DoDragDrop(idea .Item, DragDropEffects .Move Or
DragDropEffects .Copy)
End If
End Sub
Private Sub PictureBoxOnMou seDown(ByVal obj As Object, _
ByVal mea As MouseEventArgs)
If mea.Button = MouseButtons.Le ft Then
Dim image As PictureBox = CType(obj, PictureBox)
If Not image.Image Is Nothing Then
image.DoDragDro p(image.Image, DragDropEffects .Move Or _
DragDropEffects .Copy)
End If
End If
End Sub
Private Sub PictureBoxOnDra gEnter(ByVal obj As Object, _
ByVal dea As DragEventArgs)
If (dea.Data.GetDa taPresent(DataF ormats.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 PictureBoxOnDra gDrop(ByVal obj As Object, _
ByVal dea As DragEventArgs)
Dim picture As PictureBox = CType(obj, PictureBox)
picture.Image = CType(dea.Data. GetData(DataFor mats.Bitmap), Bitmap)
If (dea.KeyState And ctrlPressed) <> ctrlPressed Then
If picture.Name = "toPictureB ox" Then
fromPictureBox. Image = Nothing
Else
toPictureBox.Im age = Nothing
End If
End If
End Sub
Private Sub ExitOnClick(ByV al obj As Object, ByVal ea As EventArgs)
Application.Exi t()
End Sub
#End Region
End Class
\\\

HTH
Nov 21 '05 #2

in tree dragover event:


Dim mousePos As Point

mousePos = TreeView.PointT oClient(Cursor. Position)

Dim nodeOver As TreeViewNode = TreeView.GetNod eAt(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*********@Ei rcom.Net> wrote in message
news:O5******** *****@TK2MSFTNG P15.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*********@Ei rcom.Net> wrote in message
news:O5******** *****@TK2MSFTNG P15.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
1737
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 control. The operation takes place entirely within this single control. There is no need to provide for interaction with other controls, forms,...
1
1191
by: '[] WiRaN | last post by:
anywhere user drag and drop in treeview??? please, send me example... Wiran
1
1101
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 have set AllowDrop to true and added an event handler for DragDrop and in that handler added a MessageBox to alert me that something was dropped. ...
1
9191
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
4917
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. However, either it only add it to the root level or it only add it on level below, doesn't matter what I select. And in some case, I just get an...
3
1406
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 sub-class ? If no, any related information/reference ? Thanks a lot
1
1863
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
4080
by: dotnetnari | last post by:
hi, How to Drag and drop the nodes from treeview into datatable columns using windowsapplication in c# Thanks
3
4697
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 is if I close the application and then go back in, the nodes are back in the order they were before I had reordered them. Is there a way that I can...
1
7874
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6522
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3798
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1389
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.