Tuesday, February 22, 2011

ต้องการให้เปิดฟอร์มแล้วเลือก Default Node ที่ต้องการ ใน Treeview

For i As Integer = 0 To TreeView1.Nodes.Count - 1
If TreeView1.Nodes(i).Text = "Nodeแม่" Then
TreeView1.Nodes(i).Expand()
For j As Integer = 0 To TreeView1.Nodes(i).Nodes.Count - 1
If TreeView1.Nodes(i).Nodes(j).Text = "Nodeลูกที่ต้องการ" Then
TreeView1.Nodes(i).Nodes(j).Expand()
End If
Next
End IfNext

1 comment:

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'look for a node with a text property value of 'Node3'
    TreeView1.SelectedNode = GetNode("Node1", TreeView1.Nodes)
    'ensure the focus set on the node
    TreeView1.Select()

    End Sub

    Private Function GetNode(ByVal Node_Text As String, ByVal parentCollection As TreeNodeCollection) As TreeNode

    Dim ret As TreeNode = Nothing
    Dim child As TreeNode

    For Each child In parentCollection 'step through the parentcollection
    If child.Text = Node_Text Then
    ret = child
    ElseIf child.GetNodeCount(False) > 0 Then ' if there is child items then call this function recursively
    ret = GetNode(Node_Text, child.Nodes)
    End If

    If Not ret Is Nothing Then Exit For 'if something was found, exit out of the for loop

    Next

    Return ret

    End Function

    ReplyDelete