winform TreeView树节点上下移动

         /// <summary>
        /// 上移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbMoveUp_Click(object sender, EventArgs e)
        {
            if (Tv_tree.SelectedNode == null)
            {
                MessageBox.Show("请选择需要移动的的节点!");
                this.Focus();
                return;
            }
            folder  model = new folder();
            TreeNode node = Tv_tree.SelectedNode;     //获得选中节点的值
            model.folderId = node.Tag.ToString();
            model.TheOrder = node.Name.ToString();
            if (node.PrevNode == null)      //选中节点的上一个节点为Null则返回
            {
                return;
            }
            folder upModel = new folder();
            TreeNode upNode = node.PrevNode;            //获得选中节点的上一个节点
            upModel.folderId = upNode.Tag.ToString();
            upModel.TheOrder = upNode.Name;
            bool flag = folderOperation.MoveUp(model, upModel);       //将两个节点的排序编号互换
            if (flag)       //如果执行成功
            {
                string theOrder = node.Name;
                node.Name = upNode.Name;
                upNode.Name = theOrder;
                TreeNode newNode = (TreeNode)node.Clone();      //克隆选中的节点
                if (node.Parent == null)
                {
                    Tv_tree.Nodes.Insert(upNode.Index, newNode);      //在选中节点的上一个节点的地方插入本节点
                }
                else
                {
                    node.Parent.Nodes.Insert(upNode.Index, newNode);
                }
                node.Remove();
                Tv_tree.SelectedNode = newNode;
            }
            else
            {
                return;
            }
        }
        /// <summary>
        /// 下移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbMoveDown_Click(object sender, EventArgs e)
        {
            if (Tv_tree.SelectedNode == null)
            {
               MessageBox.Show("请选择需要移动的的节点!");
                this.Focus();
                return;
            }
            folder model = new folder();
            TreeNode node = Tv_tree.SelectedNode;     //选中的节点
            model.folderId = node.Tag.ToString();
            model.TheOrder = node.Name.ToString();
            if (node.NextNode == null)      //下一个节点为Null时返回
            {
                return;
            }
            folder downModel = new folder();
            TreeNode dowNode = node.NextNode;
            downModel.folderId = dowNode.Tag.ToString();
            downModel.TheOrder = dowNode.Name;
            bool flag = folderOperation.MoveUp(model, downModel);
            if (flag)
            {
                string theOrder = node.Name;
                node.Name = dowNode.Name;
                dowNode.Name = theOrder;
                TreeNode newNode = (TreeNode)dowNode.Clone();       //克隆下一个节点及其所有的子节点
                if (node.Parent == null)
                {
                    Tv_tree.Nodes.Insert(node.Index, newNode);    //在原节点的位置插入下一个节点的值
                }
                else
                {
                    node.Parent.Nodes.Insert(node.Index, newNode);
                }
                dowNode.Remove();           //删除掉选中节点的下一个节点
                Tv_tree.SelectedNode = node;  //重新选中
            }
            else
            {
                return;
            }
        }

 string sql = string.Format("update folder set theorder = ‘{0}‘ where folderid = ‘{1}‘", model.TheOrder, upModel.folderId);
 string sql1 = string.Format("update folder set theorder = ‘{0}‘ where folderid = ‘{1}‘", upModel.TheOrder, model.folderId);
时间: 2024-10-25 08:46:14

winform TreeView树节点上下移动的相关文章

C# TreeView 树节点:递归显示整个系统盘符文件目录及文件

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace Demo { /// <summary> /// 有关对于TreeView节点的填充的相关处理类 /// </summary> class TreeViewUtils { #region 有关将整个系统盘

winform treeview 通过节点名称添加子节点

/// <summary> /// 添加人员节点(利用TreeNodeCollection) /// </summary> /// <param name="tnc">TreeNodeCollection(TreeView的节点集合)</param> /// <param name="pid_val">父id的值</param> /// <param name="id"

TreeView 树节点的处理

TreeView 树节点的处理 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10

delphi 递归遍历TreeView树节点

//以下全部调试通过,你在窗体扔一个Treeview就行了 procedure GetRootNodes(ATreeView:TTreeView);//得到所有根节点var vNode:TTreeNode;begin  vNode:=nil;  vNode:=ATreeView.Items.GetFirstNode;  While vNode<>nil do  begin    ShowMessage(vNode.Text);//处理查找到的根节点    vNode:=vNode.getNex

[WinForm]TreeView 检查节点文本是否存在

关键代码: #region 检查节点文本是否存在 /// <summary> /// 检查节点文本是否存在 /// </summary> /// <param name="tree">TreeView</param> /// <param name="key">节点文本</param> /// <returns>是否存在</returns> public static b

c# winform TreeView NODE(节点) 重命名或获取节点修改后的值

在程序开发过程中我们经常用到treeview,还经常要修改节点的名字.节点名字修改后还想及时更新数据库.这时问题就来了,怎样获取NODE(节点)更新后的值呢?本人试了很多方法最终分析出treeview节点名字修改的AfterLabelEdit事件.事件内有两个参数如下代码 sender 和 e. private void treeView2_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { } 代码内的e 有两个值,e.Lable

Access Treeview树节点代码一

Private Sub TreeView0_Updated(Code As Integer)Dim ndeindex As NodeSet ndeindex = TreeView0.Nodes.Add(, , "a", "基础资料 ", "k1")Set ndeindex = TreeView0.Nodes.Add("a", tvwChild, "a1", "品号资料 维护 ", &qu

TreeView 树菜单 节点

<1>TreeView树的基本操作 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TreeVie

树节点

private void zimulu(TreeNode node)//在点开节点的一瞬间,解析当前目录的第三级目录 { try//对错误或者权限不作处理 { //根据获得的完整目录得到该目录下的子目录 foreach (var dirinfor in new DirectoryInfo(node.Tag.ToString()).GetDirectories()) { //依据目录的完整目录获取其子目录,否则找不到文件 //以子目录的文件名为名称创建树节点 var n = new TreeNod