TreeView Positioning

  • Thread starter Thread starter Ivan Gibbs
  • Start date Start date
I

Ivan Gibbs

I have a treeview to show the status of a nework system.
Periodically, my application receives an update on the
status of the system and the treeview is updated. After
the update, the treeview resets its' scrollbar to the top
position. How can the treeview preserve the original
scroll bar position that it was at before the update?
 
Ivan Gibbs said:
I have a treeview to show the status of a nework system.
Periodically, my application receives an update on the
status of the system and the treeview is updated. After
the update, the treeview resets its' scrollbar to the top
position. How can the treeview preserve the original
scroll bar position that it was at before the update?

How do you update the treeview control? Instead of removing the nodes and
re-adding tham, you can change their text.
 
position. How can the treeview preserve the original
scroll bar position that it was at before the update?

You could try this:

Before refreshing the treeview, use the TreeView's TopNode property to
remember the first visible node. After refreshing the TreeView, ensure
that the same node is at the top by first calling EnsureVisible() on
the last node in the TreeView, then calling EnsureVisible() for the top
node that you remembered from before the refresh.

The downside to this approach is a slight flicker.

Psuedocode:

TreeNode topNode = tree.TopNode;
RefreshTree();
tree.Nodes[ tree.Nodes.Count - 1].EnsureVisible();
topNode.EnsureVisible();


Of course you'll probably want to remember the topNode's text or some
other property, rather than the TreeNode reference itself, since you
might be destroying and recreating nodes during the RefreshTree() call.
Additionally, you'll want a better method of determining the last tree
node, since the above code only returns the last node in the root
collection.
 
Thanks for this suggestion! It has saved me from a
toublesome rewrite.

Ivan
-----Original Message-----
position. How can the treeview preserve the original
scroll bar position that it was at before the update?

You could try this:

Before refreshing the treeview, use the TreeView's TopNode property to
remember the first visible node. After refreshing the TreeView, ensure
that the same node is at the top by first calling EnsureVisible() on
the last node in the TreeView, then calling EnsureVisible () for the top
node that you remembered from before the refresh.

The downside to this approach is a slight flicker.

Psuedocode:

TreeNode topNode = tree.TopNode;
RefreshTree();
tree.Nodes[ tree.Nodes.Count - 1].EnsureVisible();
topNode.EnsureVisible();


Of course you'll probably want to remember the topNode's text or some
other property, rather than the TreeNode reference itself, since you
might be destroying and recreating nodes during the RefreshTree() call.
Additionally, you'll want a better method of determining the last tree
node, since the above code only returns the last node in the root
collection.

.
 
Back
Top