Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node is partially cut off in winform TreeView

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.

enter image description here

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?

like image 938
Smeiff Avatar asked Sep 06 '12 13:09

Smeiff


People also ask

Which property in the TreeView control represent the node?

The TreeNode class represents a node of a TreeView.

How many root nodes can a TreeView control have?

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.

How do I know if TreeView node is selected?

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.

What is TreeView node?

The Nodes property holds a collection of TreeNode objects, each of which has a Nodes property that can contain its own TreeNodeCollection.


2 Answers

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.

like image 158
Saeid Yazdani Avatar answered Oct 20 '22 02:10

Saeid Yazdani


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];
like image 1
Treb Avatar answered Oct 20 '22 01:10

Treb