C#中treeview的遍历问题

C#中treeview的遍历问题,第1张

节点和子节点需要分开遍历(当然也可以用循环嵌套),表示节点的子节点:this.trv_tree.nodes[0].nodes[0].text//第一个父节点下的第一个子节点的text值

SelectByID(this.TreeView1,this.TextBox1.Text.ToString())

private void SelectByID(TreeView tv,string ID)

{

foreach(TreeNode tn in tv.Nodes)

{

if(rec(tv,tn,ID))

{

break

}

}

}

private bool rec(TreeView tv,TreeNode tn,string ID)

{

string ss=tn.Text

if(tn.ID==ID)

{

tv.SelectedNodeIndex=tn.GetNodeIndex()

return true

}

else

{

bool t=false

foreach(TreeNode ctn in tn.Nodes)

{

if(rec(tv,ctn,ID))

{

t=true

break

}

}

return t

}

}

或者(遍历选中节点)

private void GetTreeSelected(TreeNodeCollection trees)

{

foreach(TreeNode tn in trees)

{

if(tn.Checked)

{

//执行相应的 *** 作

}

if(tn.Nodes.count!=0)

GetTreeSelected(tn.Nodes)

}

}

Private Sub Command1_Click()

Dim i As Integer

Dim nd As Node

List1.Clear

Set nd = TreeView1.SelectedItem.Child

For i = 1 To TreeView1.SelectedItem.Children

List1.AddItem nd.Text

Set nd = nd.Next

Next

End Sub

Private Sub Form_Load()

Dim i As Integer, j As Integer

TreeView1.LineStyle = tvwRootLines

TreeView1.LabelEdit = tvwManual

TreeView1.Nodes.Add , , "R", "R"

For i = 1 To 5

TreeView1.Nodes.Add "R", tvwChild, "A" &i, "A" &i

For j = 1 To 5

TreeView1.Nodes.Add "A" &i, tvwChild, "A" &i &"B" &j, "A" &i &"B" &j

Next

Next

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9777411.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存