C#中,通过单击上移,下移按钮移动树节点中的节点顺序的实现方法:
- public Form1()
- {
- InitializeComponent();
- }
- TreeNode preNode, nextNode, currentNode;
- int g_tag;
- string g_text;
- private void Form1_Load(object sender, EventArgs e)
- {
- TreeNode tn = new TreeNode();
- tn.Nodes.Add("北京");
- tn.Tag = 1;
- tn.Nodes.Add("湖北");
- tn.Tag = 2;
- tn.Nodes.Add("上海");
- tn.Tag = 3;
- tn.Nodes.Add("天津");
- tn.Tag = 4;
- treeView1.Nodes.Add(tn);
- }
- //上移
- private void button2_Click(object sender, EventArgs e)
- {
- currentNode = treeView1.SelectedNode;
- if (currentNode == null)
- {
- return;
- }
- else
- {
- preNode = currentNode.PrevNode;
- if (preNode == null)
- {
- return;
- }
- else
- {
- g_text= preNode.Text;
- g_tag=Convert.ToInt32( preNode.Tag);
- preNode.Tag= currentNode.Tag;
- preNode.Text = currentNode.Text;
- currentNode.Tag = g_tag;
- currentNode.Text = g_text;
- }
- }
- }
- //下移
- private void button3_Click(object sender, EventArgs e)
- {
- currentNode = treeView1.SelectedNode;
- if (currentNode == null)
- {
- return;
- }
- else
- {
- preNode = currentNode.NextNode;
- if (preNode == null)
- {
- return;
- }
- else
- {
- g_text = preNode.Text;
- g_tag = Convert.ToInt32(preNode.Tag);
- preNode.Tag = currentNode.Tag;
- preNode.Text = currentNode.Text;
- currentNode.Tag = g_tag;
- currentNode.Text = g_text;
- }
- }
- }
在窗体中拖1个treeView控件和两个button,按照上面思路实现就可以了。