/// <summary>
/// 选中父节点,其所有子节点也选中
/// </summary>
/// <param name="parentNode"></param>
private void SetChildChecked(TreeNode parentNode)
{
foreach (TreeNode tn in parentNode.ChildNodes)
{
tn.Checked = true;
if (tn.ChildNodes.Count > 0)
{
SetChildChecked(tn);
}
}
}
/// <summary>
/// 选中子节点,其父节点也选中
/// </summary>
/// <param name="childNode"></param>
private void SetParentChecked(TreeNode childNode)
{
if (childNode.Parent != null)
{
childNode.Parent.Checked = true;
SetParentChecked(childNode.Parent);
}
}
/// <summary>
/// 取消父节点,其所有子节点也取消
/// </summary>
/// <param name="parentNode"></param>
private void CancelChildChecked(TreeNode parentNode)
{
foreach (TreeNode tn in parentNode.ChildNodes)
{
tn.Checked = false;
if (tn.ChildNodes.Count > 0)
{
CancelChildChecked(tn);
}
}
}
}
遍历树
TreeNode tnRet = null;
foreach (TreeNode tn in TreeViewRegion.Nodes)
{
tnRet = FindNode( tn, yourValue );
if( tnRet != null ) break;
}
}
/// <summary>
/// 查找树节点递归算法
/// </summary>
/// <param name="tnParent"></param>
/// <param name="strValue"></param>
/// <returns></returns>
private TreeNode FindNode(TreeNode tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Text == strValue) return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.Nodes)
{
tnRet = FindNode(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}