欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TreeView控件操作开发篇之(三)

程序员文章站 2022-06-08 10:19:36
...
[size=large]1,树控件的上下左右移动。将此组件放到TreeView的keyup事件中,即可看到效果
[/size]
#region "控制树节点移动,向左右上下"

/// <summary>
/// 控件树节点移动
/// </summary>
/// <param name="Tv">TreeView控件</param>
/// <param name="e">TreeView的按键事件</param>
public void MoveSelectedNode(TreeView Tv,KeyEventArgs e)
{
if (Tv.SelectedNode==null)
{
return;
}
if (e.KeyCode==Keys.Up && e.Control==true)
{
TreeNode selNode = Tv.SelectedNode; //当前选中的节点
TreeNode tempNode =(TreeNode)Tv.SelectedNode.Clone();

if (selNode.PrevNode == null)
{
return;
}
else
{
if (selNode.Parent == null) //表示一级节点
{
Tv.Nodes.Insert(selNode.Index - 1, tempNode);
selNode.Remove();
Tv.SelectedNode = tempNode;
}
else
{
selNode.Parent.Nodes.Insert(selNode.Index - 1, tempNode);
selNode.Remove();
Tv.SelectedNode = tempNode;
}
}
}
else if (e.KeyCode==Keys.Down && e.Control==true)
{
TreeNode selNode = Tv.SelectedNode; //当前选中的节点
TreeNode tempNode =(TreeNode)Tv.SelectedNode.Clone();

if (selNode.NextNode==null)
{
return;
}
if (selNode.Parent == null) //表示一级节点
{
Tv.Nodes.Insert(selNode.Index + 2, tempNode);
}
else
{
selNode.Parent.Nodes.Insert(selNode.Index + 2, tempNode);
}
selNode.Remove();
Tv.SelectedNode = tempNode;
}
else if (e.KeyCode == Keys.Left && e.Control==true)
{

TreeNode selNode = Tv.SelectedNode; //当前选中的节点
TreeNode tempNode = (TreeNode)Tv.SelectedNode.Clone();

if (selNode.Parent==null)//表示一级节点
{
return;
}
if (selNode.Parent.Parent == null) //表示是三级节点
{
Tv.Nodes.Add(tempNode);

}
else
{
selNode.Parent.Parent.Nodes.Add(tempNode);

}
selNode.Remove();
Tv.SelectedNode = tempNode;
}
else if (e.KeyCode==Keys.Right && e.Control==true)
{

TreeNode selNode = Tv.SelectedNode; //当前选中的节点
TreeNode tempNode = (TreeNode)Tv.SelectedNode.Clone();
if (selNode.NextNode==null)
{
return;
}
selNode.NextNode.Nodes.Insert(0, tempNode);
selNode.Remove();
Tv.SelectedNode = tempNode;
}
}
#endregion


[size=large]2.查找遍历树节点,三种方法[/size]

  #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