473,387 Members | 1,362 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.

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 4835
If you put references in nodes tags you can just change text on the node
after swapping tags.

HTH
Alex
"alanrn" <al****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.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****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.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****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.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(int index)
{
treeView1.BeginUpdate();
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.EndUpdate();
}
*** 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
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...
4
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...
4
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...
4
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...
2
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. ...
0
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...
3
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....
2
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...
10
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...
6
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...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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,...
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...
0
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...

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.