467,905 Members | 1,837 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,905 developers. It's quick & easy.

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
  • viewed: 4460
Share:
4 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Christopher Jeris | last post: by
4 posts views Thread by alanrn | last post: by
4 posts views Thread by JoKur | last post: by
2 posts views Thread by Kristopher Wragg | last post: by
10 posts views Thread by John Rogers | last post: by
Sl1ver
6 posts views Thread by Sl1ver | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.