views:

293

answers:

3

Have a good day,

Please advice how can I scroll a .NET TreeView Control programmatically.

It doesn't implemented any public methods to scrolling.

Thanks in advance.

-- Murat

A: 

Just select and unselect the lowest item. That should do the trick.

Jouke van der Maas
A: 

Try this:

treeView1.TopNode = treeView1.Nodes[500];

EDIT Maybe more suggestive :-)

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 100; i++) {
            TreeNode node = treeView1.Nodes.Add(i.ToString());
            for (int j = 0; j < 10; j++) {
                node.Nodes.Add(j.ToString());
            }
        }
    }

    private void ScrollNode(TreeNode node) {
        treeView1.TopNode = node;
        treeView1.Refresh();
        System.Threading.Thread.Sleep(50);
        if (node.IsExpanded) {
            foreach (TreeNode subNode in node.Nodes)
                ScrollNode(subNode);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        treeView1.Focus();
        foreach (TreeNode node in treeView1.Nodes) {
            ScrollNode(node);
        }
    }

Greets Flo

Florian Reischl
What if there isn't 500 nodes?!
fletcher
What if you don't have 500 nodes? ;-)
Jouke van der Maas
Florian, thanks for the answer. How can I attach the external VScrollBar to TreeView?
Murat
Sorry to all for the sloppy answer! @Murat: I changed my example.
Florian Reischl
Florian, many thanks for your time and for the sample. But how can I sync a VScrollBarControl on a Form and TreeView control?So I can scroll TreeView by my Custom VScrollBar control.
Murat
+2  A: 

If you need to scroll to the node:

TreeNode.EnsureVisible();
Orsol
+1, correct answer.
Hans Passant
yup, if you know exactly what node you are looking for then the above peice of code will programmatically scroll to the node in question.
IbrarMumtaz