How can one navigate up and navigate a selected item programmatically in a treeview in C #?

I have one tree and 2 buttons I want when click on UP_button, above Node, if I click on DOWN_button then selection should be down

+2


source to share


5 answers


From MSDN Documentation :

private void SelectNode(TreeNode node)
{
    if(node.IsSelected)
    {
        // Determine which TreeNode to select.
        switch(myComboBox.Text)
        {
            case "Previous":
                node.TreeView.SelectedNode = node.PrevNode;
                break;
            case "PreviousVisible":
                node.TreeView.SelectedNode = node.PrevVisibleNode;
                break;
            case "Next":
                node.TreeView.SelectedNode = node.NextNode;
                break;
            case "NextVisible":
                node.TreeView.SelectedNode = node.NextVisibleNode;
                break;
            case "First":
                node.TreeView.SelectedNode = node.FirstNode;
                break;
            case "Last":
                node.TreeView.SelectedNode = node.LastNode;
                break;
        }
    }
    node.TreeView.Focus();
}

      

EDIT
Of course you should use cases for "Previous" and "Next" or "Previous" and "NextVisible" in click handlers. This code assumes that you select "action" from the combo box and click the button.



EDIT 2
Just in case you are trying to move the nodes up and down (not the selection), you can use something like the following:

TreeNode sourceNode = treeView.SelectedNode;

if (sourceNode.Parent == null)
{
    return;
}

treeView.Nodes.Remove(sourceNode);
treeView.Nodes.Insert(sourceNode.Index + 1, sourceNode);

      

This will move the current node down. Note that you need to write some more code to handle some special cases (for example, what happens to the first node? Does it work at all levels of the tree?), But basically this is what I will try.

0


source


There are several useful built-in node properties that you can use, so you should use:

TreeView.SelectedNode = TreeView.SelectedNode.PrevNode;

      



and for down:

TreeView.SelectedNode = TreeView.SelectedNode.NextNode;

      

0


source


try this for UP:

    private void btnUP_Click(object sender, EventArgs e)
    {
        var tn = tv.SelectedNode;
        if(tn==null) return;

        var idx = tn.Index;
        txt.Text = "Node: " + idx;

        var par = tn.Parent;

        if(par==null) return;

        par.Nodes.Remove(tn);

        if (idx > 0)
        {
            par.Nodes.Insert(tn.Index - 1, tn);
            return;
        }

        //if you want to move int its parent parent [grand parent :) ]
        if (par.Parent!=null)
            par.Parent.Nodes.Add(tn);
    }

      

0


source


my problem is solved.
thanks for answers

private void button1_Click(object sender, EventArgs e)
{
  TreeNode node = new TreeNode();
  node = treeView1.SelectedNode;
  treeView1.SelectedNode = node.NextVisibleNode;
  node.TreeView.Focus();
}

      

0


source


For ASP.NET Treeview:

    /// <summary>
    /// MoveComponentUpLinkButton_Click
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void MoveComponentUpLinkButton_Click(object sender, EventArgs e)
    {
        // Get the selected node
        TreeNode sourceNode = this.MyTreeview.SelectedNode;
        if (sourceNode != null)
        {
            // Get the selected node parent
            TreeNode parentNode = this.MyTreeview.SelectedNode.Parent;
            if (parentNode != null)
            {
                int index = -1;

                // For each node in selected nodes parent
                for (int j = 0; j < parentNode.ChildNodes.Count; j++)
                {
                    // If we found the selected node
                    if (sourceNode == parentNode.ChildNodes[j])
                    {
                        // save index
                        index = j;
                        break;
                    }
                }

                // If node is not already at top of list
                if (index > 0)
                {
                    // Move it up
                    parentNode.ChildNodes.RemoveAt(index);
                    parentNode.ChildNodes.AddAt(index - 1, sourceNode);
                    sourceNode.Selected = true;
                }
            }
        }
    }

    /// <summary>
    /// MoveComponentDownLinkButton_Click
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void MoveComponentDownLinkButton_Click(object sender, EventArgs e)
    {
        // Get the selected node
        TreeNode sourceNode = this.MyTreeview.SelectedNode;
        if (sourceNode != null)
        {
            // Get the selected node parent
            TreeNode parentNode = this.MyTreeview.SelectedNode.Parent;
            if (parentNode != null)
            {
                int index = -1;

                // For each node in selected nodes parent
                for (int j = 0; j < parentNode.ChildNodes.Count; j++)
                {
                    // If we found the selected node
                    if (sourceNode == parentNode.ChildNodes[j])
                    {
                        // save index
                        index = j;
                        break;
                    }
                }

                // If node is not already at botton of list
                if (index < parentNode.ChildNodes.Count - 1)
                {
                    // Move it down
                    parentNode.ChildNodes.RemoveAt(index);
                    parentNode.ChildNodes.AddAt(index + 1, sourceNode);
                    sourceNode.Selected = true;
                }
            }
        }
    }

      

0


source







All Articles