寫asp.net時,經常操作 TreeView 控件。經常需要找到他的某一個節點,
操作方法是:
/// <summary>
/// 獲取 指定value值的節點
/// </summary>
/// <param name="tnParent">指定節點</param>
/// <param name="strValue">value值</param>
/// <returns></returns>
private TreeNode FindNodeByValue(TreeNode tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Value == strValue) return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNodeByValue(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
/// <summary>
/// 獲取 指定Text值的節點
/// </summary>
/// <param name="tnParent">指定節點</param>
/// <param name="strValue">Text值</param>
/// <returns></returns>
private TreeNode FindNodeByText(TreeNode tnParent, string strText)
{
if (tnParent == null) return null;
if (tnParent.Text == strText) return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNodeByText(tn, strText);
if (tnRet != null) break;
}
return tnRet;
}
調用方法:
TreeNode tnRet = null;
foreach( TreeNode tn in TreeView1.Nodes )
{
tnRet = FindNodeByValue(tn, "202020");
if( tnRet != null ) break;
}
tnRet.Text = "更改後的Text";
參考如下:
Treeview查找節點算法
在TreeView查找某一節點,通常有兩種方法,一種是遞歸的,一種不是遞歸,但都是深度優先算法。其中,非遞歸方法效率高些,而遞歸算法要簡潔一些。
第一種,遞歸算法,代碼如下:
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;
}
第二種,非遞歸算法,代碼如下:
private TreeNode FindNode( TreeNode tnParent, string strValue )
{
if( tnParent == null ) return null;
if( tnParent.Text == strValue ) return tnParent;
else if( tnParent.Nodes.Count == 0 ) return null;
TreeNode tnCurrent, tnCurrentPar;
//Init node
tnCurrentPar = tnParent;
tnCurrent = tnCurrentPar.FirstNode;
while( tnCurrent != null && tnCurrent != tnParent )
{
while( tnCurrent != null )
{
if( tnCurrent.Text == strValue ) return tnCurrent;
else if( tnCurrent.Nodes.Count > 0 )
{
//Go into the deepest node in current sub-path
tnCurrentPar = tnCurrent;
tnCurrent = tnCurrent.FirstNode;
}
else if( tnCurrent != tnCurrentPar.LastNode )
{
//Goto next sible node
tnCurrent = tnCurrent.NextNode;
}
else
break;
}
//Go back to parent node till its has next sible node
while( tnCurrent != tnParent && tnCurrent == tnCurrentPar.LastNode )
{
tnCurrent = tnCurrentPar;
tnCurrentPar = tnCurrentPar.Parent;
}
//Goto next sible node
if( tnCurrent != tnParent )
tnCurrent = tnCurrent.NextNode;
}
return null;
}
程序調用,如下:
TreeNode tnRet = null;
foreach( TreeNode tn in yourTreeView.Nodes )
{
tnRet = FindNode( tn, yourValue );
if( tnRet != null ) break;
}
代碼轉換:
Public Function findnode(ByVal parentnode As TreeNode, ByVal str As String) As TreeNode
Dim thenode As New TreeNode
If (parentnode Is Nothing) Then
Return Nothing
End If
If (parentnode.Text = str) Then
Return parentnode
End If
Dim node As New TreeNode
For Each node In parentnode.Nodes
thenode = findnode(node, str)
If thenode.Text = str Then
Exit For
End If
Next
Return thenode
End Function