我一直在這里到處尋找答案,但找不到有效的答案..
我來自 VBA 重度背景,所以我非常了解語法,但我仍然不明白 WPF 應用程式如何在 NET 6.0 中作業,哈哈。
情況如下:
我的應用程式視窗中有一個 TreeView 元素,它有兩個父元素和一個子元素。
我的 VBA 大腦邏輯會說類似
x = 0
Do While x < TreeViewObject.Items(x).Count ' This would be the iteration for finding out parents
y = 0
Do While y < TreeViewObject.Items(x).Children.Count ' This would be the iteration for finding out how many children the parents have.
Msgbox(TreeViewObject.Items(x).Children(y)) ' prints out the children's values
y = y 1
Loop
x = x 1
Loop
..會作業,但這里的邏輯比我預期的要糟糕得多,我如何遍歷 TreeView 元素?
uj5u.com熱心網友回復:
這演示了使用遞回遍歷 Treeview
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
IterateTV(TreeView1.Nodes(0))
End Sub
Private Sub IterateTV(nd As TreeNode)
Debug.Write("' ")
Debug.WriteLine(nd.Text)
For Each tnd As TreeNode In nd.Nodes
IterateTV(tnd)
Next
End Sub
'TreeView1
' root
' A
' B
' B sub 1
' B sub 2
' B sub 2 sub a
' B sub 3
' C
uj5u.com熱心網友回復:
不管我以前的帖子,這個實作是完全優越的:
For Each VarParent In CategoryList.Items
MsgBox(VarParent.ToString)
For Each VarChild In VarParent.Items
MsgBox(VarChild.ToString)
Next
Next
此方法將遍歷每個父級,然后遍歷它擁有的每個子級,其邏輯類似于您在 VBA 中會發現的邏輯,但有 0 個額外的并發癥。
舊代碼:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
IterateTV(CategoryList)
End Sub
Private Sub IterateTV(nd)
For Each tnd In nd.Items
IterateTV(tnd)
MsgBox(tnd.ToString)
Next
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372423.html
