I am having issues figuring out what this issue is. I have googled around and have not found many solutions to this issue. The only "solution" I found was a hack to expand then collapse the last node.
this.Nodes[this.Nodes.Count - 1].Expand();
this.Nodes[this.Nodes.Count - 1].Collapse();
As you can see from this screen shot the last node is partially cut off and the only way to expose it is to expand the node which will cause the TreeView to re-render itself correctly.
I am pragmatically adding nodes to the TreeView. I do not know if this affects the outcome, but I have extended the TreeView to my own class so I can add a few properties and methods to it.
public class MyTreeView : TreeView
{
public void BuildTree()
{
this.Nodes.Clear();
foreach (TestSetFolder folder in Folders)
{
MyTreeNode node = new MyTreeNode();
node.Name = folder.Name;
node.Text = folder.Name;
node.Tag = folder;
node.FolderID = folder.NodeID;
node.IsPopulated = false;
this.Nodes.Add(node);
}
}
}
This is how I am adding the nodes to the list. Does anyone have a clean solution to this issue?
The TreeNode class represents a node of a TreeView.
A typical tree structure has only one root node; however, you can add multiple root nodes to the TreeView control. The Nodes property can also be used to manage the root nodes in the tree programmatically. You can add, insert, remove, and retrieve TreeNode objects from the collection.
SelectedNode; if ( tn == null ) Console. WriteLine("No tree node selected."); else Console. WriteLine("Selected tree node {0}.", tn.Name ); You can compare the returned TreeNode reference to the TreeNode you're looking for, and so check if it's currently selected.
The Nodes property holds a collection of TreeNode objects, each of which has a Nodes property that can contain its own TreeNodeCollection.
Use treeView.BeginUpdate()
and treeView.EndUpdate()
before and after any visual changes.
SuspendLayout() and ResumeLayout() also can be useful!
If you want UI update, do not add all nodes in one go! add one by one, sandwiched between begin and endupdate calls.
You can call EnsureVisible
on the TreeViewNode in question, like this:
treeView1.Nodes[treeView1.Nodes.Count - 1].EnsureVisible();
Check the MSDN entry for this method for further information.
Edit:
I think I found it. You probably have a root node, and the node you want to scroll into view is a subnode of that root node. Try this instead:
TreeNode rootNode = treeView1.Nodes[0];
TreeNode lastNode = rootNode.Nodes[rootNode.Nodes.Count - 1];
lastNode.EnsureVisible();
Or use the example from the MSDN article to get the last node:
TreeNode lastNode = treeView1.Nodes[treeView1.Nodes.Count - 1].
Nodes[treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Count - 1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With