How can move up and move down selected item progra

2019-07-23 17:02发布

问题:

i have one treeview and 2 button i want when click on UP_button, above Node selected if i am click on DOWN_button then selection should be go down

回答1:

There are some nice built in properties of the node that you can use, so for up you would use:

TreeView.SelectedNode = TreeView.SelectedNode.PrevNode;

and for down:

TreeView.SelectedNode = TreeView.SelectedNode.NextNode;


回答2:

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
You'd of course use the cases for "Previous" and "Next" or "PreviousVisible" and "NextVisible" in your button click handlers. This code assumes that you select the "action" from a combo box and push a 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 would move the current node one down. Please note that you have to write some more code to handle some special cases (e.g. what happens with the first node? Does it work on all levels of the tree?), but basically that's what I'd try.



回答3:

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's parent [grand parent :) ]
        if (par.Parent!=null)
            par.Parent.Nodes.Add(tn);
    }


回答4:

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's 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's 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;
                }
            }
        }
    }


回答5:

my problem solved.
thanx for answers

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


标签: c# treeview