Check out the Insert() method. Here's some sample code that moves nodes
up/down in the tree using a pair of buttons.
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
namespace moveTreeNode
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Button bUp;
private System.Windows.Forms.Button bDown;
private System.Windows.Forms.MainMenu mainMenu1;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.treeView1 = new System.Windows.Forms.TreeView();
this.bUp = new System.Windows.Forms.Button();
this.bDown = new System.Windows.Forms.Button();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(16, 0);
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(200, 192);
this.treeView1.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// bUp
//
this.bUp.Location = new System.Drawing.Point(72, 208);
this.bUp.Size = new System.Drawing.Size(104, 24);
this.bUp.Text = "move up";
this.bUp.Click += new
System.EventHandler(this.directionButton_Click);
//
// bDown
//
this.bDown.Location = new System.Drawing.Point(72, 240);
this.bDown.Size = new System.Drawing.Size(104, 20);
this.bDown.Text = "move down";
this.bDown.Click += new
System.EventHandler(this.directionButton_Click);
//
// Form1
//
this.Controls.Add(this.bDown);
this.Controls.Add(this.bUp);
this.Controls.Add(this.treeView1);
this.Menu = this.mainMenu1;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
TreeNode node;
for(int n = 0; n < 5; n++)
{
node = new TreeNode(n.ToString());
this.treeView1.Nodes.Add(node);
if(n == 2)
this.treeView1.SelectedNode = node;
}
this.treeView1.ExpandAll();
EnableMoveButtons();
}
void EnableMoveButtons()
{
int index = this.treeView1.SelectedNode.Index;
// don't let the user move the tree node past the beginning or
end
this.bUp.Enabled = (index - 1 >= 0);
this.bDown.Enabled = (index + 1) <= (this.treeView1.Nodes.Count
- 1);
// put focus back on the treeview so you can the selected node
this.treeView1.Focus();
}
private void directionButton_Click(object sender, System.EventArgs
e)
{
TreeNode node = this.treeView1.SelectedNode;
int index = this.treeView1.SelectedNode.Index;
int direction = (sender == this.bUp) ? -1 : +1;
// remove the node from the tree otherwise Insert() will
// end up duplicating the node.
this.treeView1.Nodes.Remove(node);
this.treeView1.Nodes.Insert(index + direction, node);
this.treeView1.SelectedNode = node;
EnableMoveButtons();
}
private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
// after a node is selected, update the move buttons
EnableMoveButtons();
}
}
}
NETCF Beta FAQ's --
http://www.gotdotnet.com/team/netcf/FAQ.aspx
This posting is provided "AS IS" with no warranties, and confers no rights.