Winforms Treeview保持滚动位置

Winforms Treeview保持滚动位置,winforms,treeview,Winforms,Treeview,我有一个有很多节点的树视图。如果我切换一个节点,treeview的滚动条将设置为底部 为了使切换的节点可见,我使用node.ensurevasible。但我不太喜欢这种方法,因为它让最终用户感到困惑 因此,我进一步研究,现在使用此处提供的代码: 这段代码的问题是,树视图的内容不会滚动。滚动条位于正确的位置,但内容不起任何作用。在单击滚动条之前,我不会滚动内容 所以,我想要实现的是,当一个树节点被切换时,我想要保持滚动位置 切换节点的代码。在本例中,节点向下移动。函数如下所示: // Check

我有一个有很多节点的树视图。如果我切换一个节点,treeview的滚动条将设置为底部

为了使切换的节点可见,我使用node.ensurevasible。但我不太喜欢这种方法,因为它让最终用户感到困惑

因此,我进一步研究,现在使用此处提供的代码:

这段代码的问题是,树视图的内容不会滚动。滚动条位于正确的位置,但内容不起任何作用。在单击滚动条之前,我不会滚动内容

所以,我想要实现的是,当一个树节点被切换时,我想要保持滚动位置

切换节点的代码。在本例中,节点向下移动。函数如下所示:

// Check a node is selected
if (tvCategories.SelectedNode == null)
    return;

// The first node may not be moved
if (IsNewRootCategorySelected())
    return;

Point ScrollPos = GetTreeViewScrollPos(tvCategories);

// Declare and instantiate the parent node
TreeNodeCollection parent = null;
if (tvCategories.SelectedNode.Parent == null)
    parent = tvCategories.Nodes;
else
    parent = tvCategories.SelectedNode.Parent.Nodes;

TreeNode selectedNode = tvCategories.SelectedNode;
int index = selectedNode.Index;

// Check there's a next node at the same level
if (tvCategories.SelectedNode.NextNode == null)
{
    // Check if the parent node has a next node
    if (tvCategories.SelectedNode.Parent != null && tvCategories.SelectedNode.Parent.NextNode != null)
    {
        // get the destination parent
        TreeNode destParent = selectedNode.Parent.NextNode;

        // remove selected node from tree view
        parent[index].Remove();

        // If selected node is a category, add the node to the first index
        if (selectedNode.Tag is Category)
        {
            destParent.Nodes.Insert(0, selectedNode);
        }

        // If selected node is a question, add node below last category
        if (selectedNode.Tag is Question)
        {
            int newIndex = 0;

            // Loop through collection to find last category
            for (int i = destParent.Nodes.Count - 1; i >= 0; i--)
            {
                if (destParent.Nodes[i].Tag is Category)
                {
                    newIndex = i + 1;
                    break;
                }
            }

            destParent.Nodes.Insert(newIndex, selectedNode);
        }

        selectedNode.Expand();
    }
}
else
{
    // Switch nodes in same level

    tvCategories.BeginUpdate();
    _loading = true;

    if (selectedNode.Tag is Category)
    {
        // Only switch category downwards when next node is a catgory
        if (selectedNode.NextNode.Tag is Category)
        {
            // Perform switch
            TreeNode switchNode = parent[index + 1];

            parent[index + 1].Remove();
            parent[index].Remove();

            parent.Insert(index, switchNode);
            parent.Insert(index + 1, selectedNode);
        }
        else if (selectedNode.NextNode.Tag is Question)
        {
            // Make the switch to another node below
            if (selectedNode.Parent.NextNode != null)
            {
                // Parent is always a category

                TreeNode categoryParent = selectedNode.Parent.NextNode;

                // Remove selected node from current parent
                parent.Remove(selectedNode);

                // Insert selected node
                categoryParent.Nodes.Insert(0, selectedNode);

            }
        }
    }
    if (selectedNode.Tag is Question)
    {
        if (selectedNode.NextNode.Tag is Question)
        {
            // Perform switch
            TreeNode switchNode = parent[index + 1];

            parent[index + 1].Remove();
            parent[index].Remove();

            parent.Insert(index, switchNode);
            parent.Insert(index + 1, selectedNode);
        }
    }
}

tvCategories.EndUpdate();
// Set focus
tvCategories.Focus();

tvCategories.SelectedNode = selectedNode;
SetTreeViewScrollPos(tvCategories, ScrollPos);
eZScroll(treeView1, ArrowDirection.Up, 1); 
这样称呼:

// Check a node is selected
if (tvCategories.SelectedNode == null)
    return;

// The first node may not be moved
if (IsNewRootCategorySelected())
    return;

Point ScrollPos = GetTreeViewScrollPos(tvCategories);

// Declare and instantiate the parent node
TreeNodeCollection parent = null;
if (tvCategories.SelectedNode.Parent == null)
    parent = tvCategories.Nodes;
else
    parent = tvCategories.SelectedNode.Parent.Nodes;

TreeNode selectedNode = tvCategories.SelectedNode;
int index = selectedNode.Index;

// Check there's a next node at the same level
if (tvCategories.SelectedNode.NextNode == null)
{
    // Check if the parent node has a next node
    if (tvCategories.SelectedNode.Parent != null && tvCategories.SelectedNode.Parent.NextNode != null)
    {
        // get the destination parent
        TreeNode destParent = selectedNode.Parent.NextNode;

        // remove selected node from tree view
        parent[index].Remove();

        // If selected node is a category, add the node to the first index
        if (selectedNode.Tag is Category)
        {
            destParent.Nodes.Insert(0, selectedNode);
        }

        // If selected node is a question, add node below last category
        if (selectedNode.Tag is Question)
        {
            int newIndex = 0;

            // Loop through collection to find last category
            for (int i = destParent.Nodes.Count - 1; i >= 0; i--)
            {
                if (destParent.Nodes[i].Tag is Category)
                {
                    newIndex = i + 1;
                    break;
                }
            }

            destParent.Nodes.Insert(newIndex, selectedNode);
        }

        selectedNode.Expand();
    }
}
else
{
    // Switch nodes in same level

    tvCategories.BeginUpdate();
    _loading = true;

    if (selectedNode.Tag is Category)
    {
        // Only switch category downwards when next node is a catgory
        if (selectedNode.NextNode.Tag is Category)
        {
            // Perform switch
            TreeNode switchNode = parent[index + 1];

            parent[index + 1].Remove();
            parent[index].Remove();

            parent.Insert(index, switchNode);
            parent.Insert(index + 1, selectedNode);
        }
        else if (selectedNode.NextNode.Tag is Question)
        {
            // Make the switch to another node below
            if (selectedNode.Parent.NextNode != null)
            {
                // Parent is always a category

                TreeNode categoryParent = selectedNode.Parent.NextNode;

                // Remove selected node from current parent
                parent.Remove(selectedNode);

                // Insert selected node
                categoryParent.Nodes.Insert(0, selectedNode);

            }
        }
    }
    if (selectedNode.Tag is Question)
    {
        if (selectedNode.NextNode.Tag is Question)
        {
            // Perform switch
            TreeNode switchNode = parent[index + 1];

            parent[index + 1].Remove();
            parent[index].Remove();

            parent.Insert(index, switchNode);
            parent.Insert(index + 1, selectedNode);
        }
    }
}

tvCategories.EndUpdate();
// Set focus
tvCategories.Focus();

tvCategories.SelectedNode = selectedNode;
SetTreeViewScrollPos(tvCategories, ScrollPos);
eZScroll(treeView1, ArrowDirection.Up, 1); 

我不确定这段代码是否能开箱即用,但我相信它会让你在解决问题的过程中走得很好

我切换一个节点,这意味着什么?我有一段代码切换一个节点。请参见startpost上的“我的编辑”