TreeView控件操作开发篇之(三)
1,树控件的上下左右移动。将此组件放到TreeView的keyup事件中,即可看到效果
#region "查找遍历树节点" /// <summary> /// 查找遍历树节点,根据节点名称 /// </summary> /// <param name="Tv">要查找的树</param> /// <param name="NodeName">查找的树的节点名称</param> /// <returns>TreeNode[]</returns> public TreeNode[] FindTreeNodeFromNodeName(TreeView Tv, string NodeName) { if (Tv.Nodes!=null) { return Tv.Nodes.Find(NodeName, true); } return null; } /// <summary> /// 查找遍历树节点,根据树节点路径 /// </summary> /// <param name="Tv">要查找的树</param> /// <param name="NodeFullPath">查找树的节点路径</param> /// <returns></returns> public TreeNode FindTreeNodeFromNodePath(TreeView Tv, string NodeFullPath) { string tempPath = string.Empty; if (NodeFullPath.LastIndexOf("\\") > 0) //节点0//节点3//节点8 { tempPath = NodeFullPath.Substring(NodeFullPath.LastIndexOf("\\") + 1); } else { tempPath = NodeFullPath; } TreeNode[] Node = FindTreeNodeFromNodeName(Tv, tempPath); foreach (TreeNode aNode in Node) { if (aNode.FullPath == NodeFullPath) { return aNode; } } return null; } /// <summary> /// 查找遍历树节点,根据树节点tag /// </summary> /// <param name="Tv">要查找的树</param> /// <param name="tag">要查找的tag值</param> /// <returns></returns> public TreeNode[] FindTreeNodeFromNodeTag(TreeView Tv, string tag) { try { TreeNode[] tNode = null; if (Tv == null && Tv.Nodes.Count == 0) { return null; } System.Collections.ArrayList aList = new System.Collections.ArrayList(); foreach (TreeNode node in Tv.Nodes) { ForTreeNode(node, tag, ref aList); } tNode = new TreeNode[aList.Count]; for (int i = 0; i < aList.Count; i++) { tNode[i] = (TreeNode)aList[i]; } return tNode; } catch { return null; } } /// <summary> /// 递归树的节点 /// </summary> /// <param name="node">要遍历的节点</param> /// <param name="tag">查找的Tag值</param> /// <param name="aList"></param> private void ForTreeNode(TreeNode node, string tag, ref System.Collections.ArrayList aList) { if (node.Tag!=null && node.Tag.ToString()==tag) { aList.Add(node);//将节点添加到数组中 } foreach (TreeNode iNode in node.Nodes) { ForTreeNode(iNode,tag,ref aList); } } #endregion