473,915 Members | 5,960 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Drag & Drop...

My first time using TreeViews. I have TreeView1 set up to display my
directory structure just like Windows Explorer. I can drag & drop files and
folders over to TreeView2. I can re-arrange the structure in TreeView2
(putting files in different folders and such...like you do when you create a
structure for burning a CD).

I have two questions.

1. How can I remember the full path from TreeView1 (when it comes time to
actually do something with the arrangement in TreeView2)?
2. I have not been able to figure out how to get the DragOver part to work
correctly. In the code below, when I drag over a file node, it's supposed
to display the "not here" cursor. When I drag over a directory node (or the
root), it's supposed to display the "drop" cursor. It does fine except when
I drag over a file node and then drag off the file node. When I drag over
the file node, the cursor changes to "not here". When I drag off the file
node, I get an exception "object reference not set to an instance, try using
the NEW keyword". Looking for help to correct this.

Thanks.
Public Sub TreeViewISO_Dra gOver(ByVal sender As System.Object, ByVal e As
DragEventArgs) Handles TreeViewISO.Dra gOver
'Check that there is a TreeNode being dragged
If e.Data.GetDataP resent("System. Windows.Forms.T reeNode", True) = False Then
Exit Sub
'Get the TreeView raising the event (incase multiple on form)
Dim selectedTreevie w As TreeView = CType(sender, TreeView)
'As the mouse moves over nodes, provide feedback to
'the user by highlighting the node that is the
'current drop target
Dim pt As Point = CType(sender, TreeView).Point ToClient(New Point(e.X, e.Y))
Dim targetNode As TreeNode = selectedTreevie w.GetNodeAt(pt)
'See if the targetNode is currently selected,
'if so no need to validate again
If Not (selectedTreevi ew.SelectedNode Is targetNode) Then
'Select the node currently under the cursor
selectedTreevie w.SelectedNode = targetNode
'Check that the selected node is not the dropNode and
'also that it is not a child of the dropNode and
'therefore an invalid target
Dim dropNode As TreeNode =
CType(e.Data.Ge tData("System.W indows.Forms.Tr eeNode"), TreeNode)
If targetNode.Imag eIndex = 2 Then <== Exception thrown here...
e.Effect = DragDropEffects .None
Else
'Currently selected node is a suitable target
e.Effect = DragDropEffects .Move
End If
End If
End Sub
Nov 21 '05 #1
1 2327

1. Im not sure what your asking here. If you just want a path then why cant
you store it in a string? If you want the tree list structure then just
clone the node thats interests you (or its root) and store a reference to
this node in a variable.

2. If Not (selectedTreevi ew.SelectedNode Is targetNode)

If target node is Nothing then the code in the conditional still executes.
You then try to access a property of Nothing which is why the excepion gets
thrown.

targetNode.Imag eIndex

I would chnage the conditional too

If Not targetNot Is Nothing AndAlso (selectedTreevi ew.SelectedNode Is
targetNode) then

RR

"Terry Olsen" <to******@hotma il.com> wrote in message
news:OY******** ******@TK2MSFTN GP14.phx.gbl...
My first time using TreeViews. I have TreeView1 set up to display my
directory structure just like Windows Explorer. I can drag & drop files and folders over to TreeView2. I can re-arrange the structure in TreeView2
(putting files in different folders and such...like you do when you create a structure for burning a CD).

I have two questions.

1. How can I remember the full path from TreeView1 (when it comes time to
actually do something with the arrangement in TreeView2)?
2. I have not been able to figure out how to get the DragOver part to work
correctly. In the code below, when I drag over a file node, it's supposed
to display the "not here" cursor. When I drag over a directory node (or the root), it's supposed to display the "drop" cursor. It does fine except when I drag over a file node and then drag off the file node. When I drag over
the file node, the cursor changes to "not here". When I drag off the file
node, I get an exception "object reference not set to an instance, try using the NEW keyword". Looking for help to correct this.

Thanks.
Public Sub TreeViewISO_Dra gOver(ByVal sender As System.Object, ByVal e As
DragEventArgs) Handles TreeViewISO.Dra gOver
'Check that there is a TreeNode being dragged
If e.Data.GetDataP resent("System. Windows.Forms.T reeNode", True) = False Then Exit Sub
'Get the TreeView raising the event (incase multiple on form)
Dim selectedTreevie w As TreeView = CType(sender, TreeView)
'As the mouse moves over nodes, provide feedback to
'the user by highlighting the node that is the
'current drop target
Dim pt As Point = CType(sender, TreeView).Point ToClient(New Point(e.X, e.Y)) Dim targetNode As TreeNode = selectedTreevie w.GetNodeAt(pt)
'See if the targetNode is currently selected,
'if so no need to validate again
If Not (selectedTreevi ew.SelectedNode Is targetNode) Then
'Select the node currently under the cursor
selectedTreevie w.SelectedNode = targetNode
'Check that the selected node is not the dropNode and
'also that it is not a child of the dropNode and
'therefore an invalid target
Dim dropNode As TreeNode =
CType(e.Data.Ge tData("System.W indows.Forms.Tr eeNode"), TreeNode)
If targetNode.Imag eIndex = 2 Then <== Exception thrown here...
e.Effect = DragDropEffects .None
Else
'Currently selected node is a suitable target
e.Effect = DragDropEffects .Move
End If
End If
End Sub

Nov 21 '05 #2

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

Similar topics

1
7657
by: Karsten Schramm | last post by:
Hi, if I drag an Outlook.MailItem to a Windows-Explorer window a <subject>.msg file will be created. Now I try to drag & drop a mail item to my own WinForm app. Unfortunately it doesn't work. In the "drop event" are eight DataFormats (e.Data.GetFormats()) but the important one (imho) "FileContents" is "Nothing"/"null".
0
2294
by: Plumer | last post by:
Hello everyone, Yesterday I posted a message about implementing drag & drop in a TreeView control. I'm having real difficulty getting this to work -- the process seems to be incredibly delicately balanced and finding my way through it has descended into one of those awful situations which I refer to as guess & test. From the point at which DoDragDrop() function is called until some later
2
9221
by: Grey | last post by:
I need to design a workflow application with C#. I want to design an UI with some workflow components which they can be drag & drop anywhere in order to design the workflow for the application users. Moreover, when the user double click on the component, they can define some routing rules for the process. Is it possible to have such fancy UI in C#?? Any reference sites or tutorial?? Million Thanks.
2
2814
by: Ivo Tcholakov | last post by:
Is it possible to drag and drop controls in an aspx page at runtime ? Meaning i have developed a ASP.NET web form, the web form is now downloaded in IE - now can i have this form to detect mouse events, and provide drag and drop functionality of the controls ? Thank You Ivo Tcholakov
6
3569
by: jojobar | last post by:
Hello, I look at the asp.net 2.0 web parts tutorial on the asp.net web site. I tried to run it under firefox browser but it did not run. If I want to use this feature in a commercial product where the user can run on firefox/mozilla, what would be a good approach. 1. Should I overwrite the javascript code drag-and-drop to make it more browser independent. If I want to go this route, can anybody provide me a
3
3896
by: Goldwind | last post by:
Hi, I"m trying to use drag & drop of text from one text box to another but without suceess. Microsoft presented an example in "101 code samples" BUT in this example the code select and drag all the text in the TextBox, wether the user wants or not. I need to drag only the selected text (changing the example causes it not to work).
3
10608
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
5
4884
by: sesar | last post by:
How can I impement drag&drop for simple 'dialog base' application... I want to drag&drop txt file and load the text to variable
5
13838
by: Romulo NF | last post by:
Greetings, I´m back here to show the new version of the drag & drop table columns (original script ). I´ve found some issues with the old script, specially when trying to use 2 tables with drag&drop on the same page (which was not possible). Now i´ve a new concept of the script, more object oriented. I´ve also commented the whole code so you guys can easier understand it engine. What exactly we need when trying to make a column drag &...
0
10039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9883
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11359
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10543
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9734
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7259
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4346
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3370
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.