473,705 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Swapping nodes in TreeView

I am using a TreeView to display the hierarchy of a strongly-typed collection
(inherited from CollectionBase) . The order of the nodes in the TreeView is
strictly tied to the order in which they appear in the underlying collection.
However, the user must be able to reorder certain items in the
collection--and, hence, the TreeView.

I have created a context-sensitive menu that allows the user to move an item
in the collection either up or down. To move the items in the collection up
or down, I have written a Swap() method that easily accomplishes this.
However, swapping the associated TreeNode nodes in the TreeView is turning
out to be problematic. There doesn't appear to be any "easy" way to do this.

The underlying reason for this seems to be that the Collection is simply
implemented as a List (IList) and, therefore, items can be easily moved
about. The TreeView, on the other hand, is (probably) implemented as a
series of linked lists. Moving nodes around in a linked list has never been
a strong point of this data structure.

Since the TreeView is populated by the underlying collection in the first
place, one solution would be to simply clear and re-populate the TreeView
after two nodes are swapped in the collection. However, this would destroy
the "visibile integrity" of the TreeView by collapsing the entire tree.

Another possibility seems to be to make "backup" copies of all the parent
nodes (and sub-nodes), delete all child nodes of the parent, and then re-add
them in the proper order. However, this seems like a lot of work and may
also end up destroying the "visible integrity" of the tree.

Does anybody have any ideas on how to (more) easily swap two sibling nodes
(and their respective sub-nodes) in a TreeView? Any ideas would be much
appreciated!!!

..ARN.
Nov 22 '05 #1
4 4861
If you put references in nodes tags you can just change text on the node
after swapping tags.

HTH
Alex
"alanrn" <al****@discuss ions.microsoft. com> wrote in message
news:A5******** *************** ***********@mic rosoft.com...
I am using a TreeView to display the hierarchy of a strongly-typed collection (inherited from CollectionBase) . The order of the nodes in the TreeView is strictly tied to the order in which they appear in the underlying collection. However, the user must be able to reorder certain items in the
collection--and, hence, the TreeView.

I have created a context-sensitive menu that allows the user to move an item in the collection either up or down. To move the items in the collection up or down, I have written a Swap() method that easily accomplishes this.
However, swapping the associated TreeNode nodes in the TreeView is turning
out to be problematic. There doesn't appear to be any "easy" way to do this.
The underlying reason for this seems to be that the Collection is simply
implemented as a List (IList) and, therefore, items can be easily moved
about. The TreeView, on the other hand, is (probably) implemented as a
series of linked lists. Moving nodes around in a linked list has never been a strong point of this data structure.

Since the TreeView is populated by the underlying collection in the first
place, one solution would be to simply clear and re-populate the TreeView
after two nodes are swapped in the collection. However, this would destroy the "visibile integrity" of the TreeView by collapsing the entire tree.

Another possibility seems to be to make "backup" copies of all the parent
nodes (and sub-nodes), delete all child nodes of the parent, and then re-add them in the proper order. However, this seems like a lot of work and may
also end up destroying the "visible integrity" of the tree.

Does anybody have any ideas on how to (more) easily swap two sibling nodes
(and their respective sub-nodes) in a TreeView? Any ideas would be much
appreciated!!!

.ARN.

Nov 22 '05 #2
I'm using the TreeNode tags to associate the node with the underlying element
from the collection.

..ARN.

"AlexS" wrote:
If you put references in nodes tags you can just change text on the node
after swapping tags.

HTH
Alex
"alanrn" <al****@discuss ions.microsoft. com> wrote in message
news:A5******** *************** ***********@mic rosoft.com...
I am using a TreeView to display the hierarchy of a strongly-typed

collection
(inherited from CollectionBase) . The order of the nodes in the TreeView

is
strictly tied to the order in which they appear in the underlying

collection.
However, the user must be able to reorder certain items in the
collection--and, hence, the TreeView.

I have created a context-sensitive menu that allows the user to move an

item
in the collection either up or down. To move the items in the collection

up
or down, I have written a Swap() method that easily accomplishes this.
However, swapping the associated TreeNode nodes in the TreeView is turning
out to be problematic. There doesn't appear to be any "easy" way to do

this.

The underlying reason for this seems to be that the Collection is simply
implemented as a List (IList) and, therefore, items can be easily moved
about. The TreeView, on the other hand, is (probably) implemented as a
series of linked lists. Moving nodes around in a linked list has never

been
a strong point of this data structure.

Since the TreeView is populated by the underlying collection in the first
place, one solution would be to simply clear and re-populate the TreeView
after two nodes are swapped in the collection. However, this would

destroy
the "visibile integrity" of the TreeView by collapsing the entire tree.

Another possibility seems to be to make "backup" copies of all the parent
nodes (and sub-nodes), delete all child nodes of the parent, and then

re-add
them in the proper order. However, this seems like a lot of work and may
also end up destroying the "visible integrity" of the tree.

Does anybody have any ideas on how to (more) easily swap two sibling nodes
(and their respective sub-nodes) in a TreeView? Any ideas would be much
appreciated!!!

.ARN.


Nov 22 '05 #3
Ever have one of those aha moments? I just reailzed what you were saying and
it makes sense. Thanks.

..ARN.

"AlexS" wrote:
If you put references in nodes tags you can just change text on the node
after swapping tags.

HTH
Alex
"alanrn" <al****@discuss ions.microsoft. com> wrote in message
news:A5******** *************** ***********@mic rosoft.com...
I am using a TreeView to display the hierarchy of a strongly-typed

collection
(inherited from CollectionBase) . The order of the nodes in the TreeView

is
strictly tied to the order in which they appear in the underlying

collection.
However, the user must be able to reorder certain items in the
collection--and, hence, the TreeView.

I have created a context-sensitive menu that allows the user to move an

item
in the collection either up or down. To move the items in the collection

up
or down, I have written a Swap() method that easily accomplishes this.
However, swapping the associated TreeNode nodes in the TreeView is turning
out to be problematic. There doesn't appear to be any "easy" way to do

this.

The underlying reason for this seems to be that the Collection is simply
implemented as a List (IList) and, therefore, items can be easily moved
about. The TreeView, on the other hand, is (probably) implemented as a
series of linked lists. Moving nodes around in a linked list has never

been
a strong point of this data structure.

Since the TreeView is populated by the underlying collection in the first
place, one solution would be to simply clear and re-populate the TreeView
after two nodes are swapped in the collection. However, this would

destroy
the "visibile integrity" of the TreeView by collapsing the entire tree.

Another possibility seems to be to make "backup" copies of all the parent
nodes (and sub-nodes), delete all child nodes of the parent, and then

re-add
them in the proper order. However, this seems like a lot of work and may
also end up destroying the "visible integrity" of the tree.

Does anybody have any ideas on how to (more) easily swap two sibling nodes
(and their respective sub-nodes) in a TreeView? Any ideas would be much
appreciated!!!

.ARN.


Nov 22 '05 #4

hi dude, i'm using this one here but have minor problems..

just try...

private void SwapElements(in t index)
{
treeView1.Begin Update();
TreeNode n1 = treeView1.Nodes[index];
TreeNode n2 = treeView1.Nodes[index - 1];
treeView1.Nodes .RemoveAt(index );
treeView1.Nodes .RemoveAt(index - 1);
treeView1.Nodes .Insert(index - 1, n1);
treeView1.Nodes .Insert(index, n2);
treeView1.Focus ();
treeView1.EndUp date();
}
*** Sent via Developersdex http://www.developersdex.com ***
Nov 22 '05 #5

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

Similar topics

3
2082
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I would also appreciate any general criticism you have to offer. I don't know yet how to write the degradation-path code for browsers that don't support the DOM methods I'm using, so there are some commented-out paths below. If the content...
4
1285
by: alanrn | last post by:
I am using a TreeView to display the hierarchy of a strongly-typed collection (inherited from CollectionBase). The order of the nodes in the TreeView is strictly tied to the order in which they appear in the underlying collection. However, the user must be able to reorder certain items in the collection--and, hence, the TreeView. I have created a context-sensitive menu that allows the user to move an item in the collection either up or...
4
24250
by: JoKur | last post by:
I'm currently using a context menu to allow the user to add, rename and delete nodes in a treeview control. When a node is added we give it a bogus name ("NewNode") and invoke the add method. We would then to then select the newly added node and rename it but can't figure out how to select the node via code. How do you select a node from code when you don't know the index? -- Thanks
4
2312
by: pmcguire | last post by:
I have a treeview with a lot of nodes. I want to load only the nodes that are initially visible when the form loads, and then continue to populate it in background and/or when the nodes are required by the user either scrolling or performing some other action that would move the treeview window to a particular "unloaded" node in the treeview Any advice on how to go about this? It seems I need a way of sensing a scroll event in the treeview...
2
22041
by: Jack | last post by:
Hello, I am trying use a TreeView with checkboxes. I would like to check more than one node and allow all child nodes of selected nodes to be checked or unchecked with the parent is checked. Thanks in advance for any help, Jack
0
1466
by: stardust | last post by:
Hi everyone, Within a Winform application, a treeview is contained in a User Control and the User Control is then located within a splitview. When the UserControl added some nodes into the Treeview, the treeview is not displaying the added nodes, it just keep displaying the initial node added to it during initallisation. Tried variious method like TreeView.Invalidate() and TreeView.Update(). The code which adds nodes to the treeview is...
3
12142
by: juvi | last post by:
Hi, I have got a problem with Treeview.Nodes.Clear() under VB2005. When I have some nodes in my treeview and a force to clear() all nodes then it seems to work, because the nodes are not visible. But when I add new nodes and I want to look for a specific item then I only get the Items from the beginning --> something is not working with the clear() method. Please help me!?! juvi
2
2257
by: Kristopher Wragg | last post by:
I'm having some serious problems with the TreeView control. I've got a control that inherits TreeView and has some methods that firstly create a TreeNode then does some recursive procedure to add all the children from a database of a sort. Then once this is complete I clear the nodes, then add the TreeNode so it should be the only root node. The only problem is that for some very VERY strange reason there are two root nodes, with...
10
34505
by: John Rogers | last post by:
This code only counts the parent nodes or rootnodes in a treeview, how do you count all the nodes in a treeview? // one way int NodeCounter = 0; foreach (TreeNode currentNode in TreeView1.Nodes) NodeCounter++; // other way int total = TreeView1.Nodes.Count;
6
4061
Sl1ver
by: Sl1ver | last post by:
I've got a problem, i got the nodes to move but 1. they copy nodes(if you drag it to 3 different places it will actually have 3 of the same nodes) 2. i want to make the nodes, if moved update the new nodes locations This is my coding private void tvDescriptions_MouseDown(object sender, MouseEventArgs e) { TreeView tree = (TreeView)sender;
0
8767
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
9273
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...
1
9030
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8979
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
7893
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
4439
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4703
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3137
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
2
2486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.