469,646 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

how to select next node in treeview control


I have a form with treeview control loaded from xml document,text box, two
buttons named "Find" and "FindNext" and my treeview which looks like below.
Details
|__ policy status
|__ created by
|__ cover type
Risk address
|__Line1
|__Line2
|__Line3
|__Line4
Proposers
|__ForeName
|__Initials
|__Surname
|__Company
|__DOB

Drivers
|__ForeName
|__Initials
|__Surname
|__Company
when user enters some text in text box like "Surname" and click "find"
button i need to select(highlight) the "surname" node in "proposers" node in
treeview which i have done by using the following code.

Dim blnSelectNode As Boolean = False

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFind.Click
Try
Dim textval As String = ""
textval = Trim(txtFind.Text) 'textbox value
If Len(textval) = 0 Then
MsgBox("Please enter some text to find")
txtFind.Focus()
Else
' write code to find nodes or childs

blnSelectNode = False
LoopParents(textval, Me.TreeView1.Nodes)
If Not blnSelectNode Then MsgBox("Node not found",
MsgBoxStyle.Information, "Document Manager")
End If

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub LoopParents(ByVal sValue As String, ByVal oNodes As
TreeNodeCollection)
Dim tnode
For Each tnode In oNodes
If InStr(LCase(tnode.Text), sValue) > 0 Then
Me.TreeView1.SelectedNode = tnode
Me.TreeView1.Select()
blnSelectNode = True
Exit Sub
End If
If Not blnSelectNode Then LoopChilds(tnode, sValue)
Next
End Sub
Private Sub LoopChilds(ByVal PNode As TreeNode, ByVal sValue As String)
Dim TNode As TreeNode
For Each TNode In PNode.Nodes
If InStr(LCase(TNode.Text), sValue) > 0 Then
Me.TreeView1.SelectedNode = TNode
Me.TreeView1.Select()
blnSelectNode = True
Exit Sub
End If
Next
End Sub


Now i need to select the next matching node(if found) i.e "surname" in
"Drivers" node when user clicks the "findnext" button. I am not sure how to
do this one. All i need to do is select(highlight) the next matching node in
treeview control.

Any help would be appreciated
Cheers

Praveen

Apr 25 '06 #1
4 6687

Me.TreeView1.SelectedNode = tnode should be selecting the node of interest
for you. To "scroll" that node into view use tnode.ensurevisible.

"praveen" <pr*****@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...

I have a form with treeview control loaded from xml document,text box, two
buttons named "Find" and "FindNext" and my treeview which looks like
below.
Details
|__ policy status
|__ created by
|__ cover type
Risk address
|__Line1
|__Line2
|__Line3
|__Line4
Proposers
|__ForeName
|__Initials
|__Surname
|__Company
|__DOB

Drivers
|__ForeName
|__Initials
|__Surname
|__Company
when user enters some text in text box like "Surname" and click "find"
button i need to select(highlight) the "surname" node in "proposers" node
in
treeview which i have done by using the following code.

Dim blnSelectNode As Boolean = False

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFind.Click
Try
Dim textval As String = ""
textval = Trim(txtFind.Text) 'textbox value
If Len(textval) = 0 Then
MsgBox("Please enter some text to find")
txtFind.Focus()
Else
' write code to find nodes or childs

blnSelectNode = False
LoopParents(textval, Me.TreeView1.Nodes)
If Not blnSelectNode Then MsgBox("Node not found",
MsgBoxStyle.Information, "Document Manager")
End If

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub LoopParents(ByVal sValue As String, ByVal oNodes As
TreeNodeCollection)
Dim tnode
For Each tnode In oNodes
If InStr(LCase(tnode.Text), sValue) > 0 Then
Me.TreeView1.SelectedNode = tnode
Me.TreeView1.Select()
blnSelectNode = True
Exit Sub
End If
If Not blnSelectNode Then LoopChilds(tnode, sValue)
Next
End Sub
Private Sub LoopChilds(ByVal PNode As TreeNode, ByVal sValue As String)
Dim TNode As TreeNode
For Each TNode In PNode.Nodes
If InStr(LCase(TNode.Text), sValue) > 0 Then
Me.TreeView1.SelectedNode = TNode
Me.TreeView1.Select()
blnSelectNode = True
Exit Sub
End If
Next
End Sub


Now i need to select the next matching node(if found) i.e "surname" in
"Drivers" node when user clicks the "findnext" button. I am not sure how
to
do this one. All i need to do is select(highlight) the next matching node
in
treeview control.

Any help would be appreciated
Cheers

Praveen

Apr 25 '06 #2
Hi AMDRI,

I want to select the next matching node in treeview control. How can i do
that ?
Cheers

Praveen
"AMDRIT" wrote:

Me.TreeView1.SelectedNode = tnode should be selecting the node of interest
for you. To "scroll" that node into view use tnode.ensurevisible.

"praveen" <pr*****@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...

I have a form with treeview control loaded from xml document,text box, two
buttons named "Find" and "FindNext" and my treeview which looks like
below.
Details
|__ policy status
|__ created by
|__ cover type
Risk address
|__Line1
|__Line2
|__Line3
|__Line4
Proposers
|__ForeName
|__Initials
|__Surname
|__Company
|__DOB

Drivers
|__ForeName
|__Initials
|__Surname
|__Company
when user enters some text in text box like "Surname" and click "find"
button i need to select(highlight) the "surname" node in "proposers" node
in
treeview which i have done by using the following code.

Dim blnSelectNode As Boolean = False

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFind.Click
Try
Dim textval As String = ""
textval = Trim(txtFind.Text) 'textbox value
If Len(textval) = 0 Then
MsgBox("Please enter some text to find")
txtFind.Focus()
Else
' write code to find nodes or childs

blnSelectNode = False
LoopParents(textval, Me.TreeView1.Nodes)
If Not blnSelectNode Then MsgBox("Node not found",
MsgBoxStyle.Information, "Document Manager")
End If

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub LoopParents(ByVal sValue As String, ByVal oNodes As
TreeNodeCollection)
Dim tnode
For Each tnode In oNodes
If InStr(LCase(tnode.Text), sValue) > 0 Then
Me.TreeView1.SelectedNode = tnode
Me.TreeView1.Select()
blnSelectNode = True
Exit Sub
End If
If Not blnSelectNode Then LoopChilds(tnode, sValue)
Next
End Sub
Private Sub LoopChilds(ByVal PNode As TreeNode, ByVal sValue As String)
Dim TNode As TreeNode
For Each TNode In PNode.Nodes
If InStr(LCase(TNode.Text), sValue) > 0 Then
Me.TreeView1.SelectedNode = TNode
Me.TreeView1.Select()
blnSelectNode = True
Exit Sub
End If
Next
End Sub


Now i need to select the next matching node(if found) i.e "surname" in
"Drivers" node when user clicks the "findnext" button. I am not sure how
to
do this one. All i need to do is select(highlight) the next matching node
in
treeview control.

Any help would be appreciated
Cheers

Praveen


Apr 25 '06 #3
Here is something I threw together. Maybe it will help you out? Given a
form, add a treeview, and 3 buttons.
Private mSurnames As ArrayList
Private mGivennames As ArrayList

Private mFoundNodes As ArrayList
Private mFoundCurrentIndex As Integer = -2

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.Button1.Text = "&Load Data"
Me.Button2.Text = "&Find Data"
Me.Button3.Text = "&Find Next"

mSurnames = New ArrayList(New String() {"Smith", "Jones", "Baker",
"Adams", "Holms", "Green", "Halliwell"})
mGivennames = New ArrayList(New String() {"Tom", "Dick", "Harry", "Bob",
"Tina", "Julie", "Piper"})

With Me.TreeView1
.FullRowSelect = True
.Indent = 5
.ShowRootLines = True
.ShowPlusMinus = True
.ShowLines = True
End With

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Load Data (arbitrary data)

Me.TreeView1.Nodes.Clear()

For i As Integer = 1 To 100
Dim oNode As TreeNode
oNode = Me.TreeView1.Nodes.Add(String.Format("Driver {0}", i))
CreateChildHive(oNode)
Next

End Sub

Private Sub CreateChildHive(ByVal oNode As TreeNode)

'Remove all child nodes
Do Until oNode.FirstNode Is Nothing
Me.TreeView1.Nodes.Remove(oNode.FirstNode)
Loop

Dim iTemp As Integer
Dim iDOB As Integer

iTemp = CInt(Int((mSurnames.Count * Rnd()) + 1))
oNode.Nodes.Add(mSurnames.Item(iTemp - 1).ToString)

iTemp = CInt(Int((mGivennames.Count * Rnd()) + 1))
oNode.Nodes.Add(mGivennames.Item(iTemp - 1).ToString)

iTemp = DateDiff(DateInterval.Day, DateAdd(DateInterval.Year, -16, Now),
DateAdd(DateInterval.Year, -75, Now))
iDOB = CInt(Int((iTemp * Rnd()) + 1))
oNode.Nodes.Add(DateAdd(DateInterval.Day, iDOB, Now).ToShortDateString)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

FindNodes()

End Sub

Private Sub FindNodes()

mFoundCurrentIndex = -2

Dim searchFor As String
searchFor = InputBox("What are you looking for?")

If searchFor.Length > 0 Then
mFoundNodes = WalkTree(searchFor, Me.TreeView1.Nodes)
Else
Exit Sub
End If

'mFoundNodes = Me.TreeView1.Nodes.Find("Holms", True)

If Not mFoundNodes Is Nothing AndAlso mFoundNodes.Count > 0 Then
mFoundCurrentIndex = -1

FindNext()

Else
'Set to no nodes
mFoundCurrentIndex = -2
End If

End Sub

Private Function WalkTree(ByVal searchFor As String, ByVal PNodes As
TreeNodeCollection) As ArrayList

Dim oRet As ArrayList, iCount As Integer
Dim iTemp As ArrayList

oRet = New ArrayList
iCount = -1

For Each node As TreeNode In PNodes
If node.Text.Contains(searchFor) Then
oRet.Add(node)
End If

iTemp = WalkTree(searchFor, node.Nodes)
oRet.AddRange(iTemp.ToArray)

Next

Return oRet

End Function

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
FindNext()
End Sub

Private Sub FindNext()

If mFoundCurrentIndex >= -1 Then

mFoundCurrentIndex += 1

If Not mFoundNodes Is Nothing AndAlso mFoundNodes.Count - 1 >=
mFoundCurrentIndex Then
Me.TreeView1.SelectedNode = mFoundNodes(mFoundCurrentIndex)
mFoundNodes(mFoundCurrentIndex).EnsureVisible()
Else
mFoundCurrentIndex = -1
FindNext()
End If

Debug.Print(Me.TreeView1.SelectedNode.Text)

End If
End Sub
Apr 25 '06 #4
Hi AMDRIT

Its brilliant and it works fine. You rescued me...

Thanks a lot

Praveen

"AMDRIT" wrote:
Here is something I threw together. Maybe it will help you out? Given a
form, add a treeview, and 3 buttons.
Private mSurnames As ArrayList
Private mGivennames As ArrayList

Private mFoundNodes As ArrayList
Private mFoundCurrentIndex As Integer = -2

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.Button1.Text = "&Load Data"
Me.Button2.Text = "&Find Data"
Me.Button3.Text = "&Find Next"

mSurnames = New ArrayList(New String() {"Smith", "Jones", "Baker",
"Adams", "Holms", "Green", "Halliwell"})
mGivennames = New ArrayList(New String() {"Tom", "Dick", "Harry", "Bob",
"Tina", "Julie", "Piper"})

With Me.TreeView1
.FullRowSelect = True
.Indent = 5
.ShowRootLines = True
.ShowPlusMinus = True
.ShowLines = True
End With

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Load Data (arbitrary data)

Me.TreeView1.Nodes.Clear()

For i As Integer = 1 To 100
Dim oNode As TreeNode
oNode = Me.TreeView1.Nodes.Add(String.Format("Driver {0}", i))
CreateChildHive(oNode)
Next

End Sub

Private Sub CreateChildHive(ByVal oNode As TreeNode)

'Remove all child nodes
Do Until oNode.FirstNode Is Nothing
Me.TreeView1.Nodes.Remove(oNode.FirstNode)
Loop

Dim iTemp As Integer
Dim iDOB As Integer

iTemp = CInt(Int((mSurnames.Count * Rnd()) + 1))
oNode.Nodes.Add(mSurnames.Item(iTemp - 1).ToString)

iTemp = CInt(Int((mGivennames.Count * Rnd()) + 1))
oNode.Nodes.Add(mGivennames.Item(iTemp - 1).ToString)

iTemp = DateDiff(DateInterval.Day, DateAdd(DateInterval.Year, -16, Now),
DateAdd(DateInterval.Year, -75, Now))
iDOB = CInt(Int((iTemp * Rnd()) + 1))
oNode.Nodes.Add(DateAdd(DateInterval.Day, iDOB, Now).ToShortDateString)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

FindNodes()

End Sub

Private Sub FindNodes()

mFoundCurrentIndex = -2

Dim searchFor As String
searchFor = InputBox("What are you looking for?")

If searchFor.Length > 0 Then
mFoundNodes = WalkTree(searchFor, Me.TreeView1.Nodes)
Else
Exit Sub
End If

'mFoundNodes = Me.TreeView1.Nodes.Find("Holms", True)

If Not mFoundNodes Is Nothing AndAlso mFoundNodes.Count > 0 Then
mFoundCurrentIndex = -1

FindNext()

Else
'Set to no nodes
mFoundCurrentIndex = -2
End If

End Sub

Private Function WalkTree(ByVal searchFor As String, ByVal PNodes As
TreeNodeCollection) As ArrayList

Dim oRet As ArrayList, iCount As Integer
Dim iTemp As ArrayList

oRet = New ArrayList
iCount = -1

For Each node As TreeNode In PNodes
If node.Text.Contains(searchFor) Then
oRet.Add(node)
End If

iTemp = WalkTree(searchFor, node.Nodes)
oRet.AddRange(iTemp.ToArray)

Next

Return oRet

End Function

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
FindNext()
End Sub

Private Sub FindNext()

If mFoundCurrentIndex >= -1 Then

mFoundCurrentIndex += 1

If Not mFoundNodes Is Nothing AndAlso mFoundNodes.Count - 1 >=
mFoundCurrentIndex Then
Me.TreeView1.SelectedNode = mFoundNodes(mFoundCurrentIndex)
mFoundNodes(mFoundCurrentIndex).EnsureVisible()
Else
mFoundCurrentIndex = -1
FindNext()
End If

Debug.Print(Me.TreeView1.SelectedNode.Text)

End If
End Sub

Apr 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by G Uljee | last post: by
2 posts views Thread by Stephen | last post: by
13 posts views Thread by André Nogueira | last post: by
2 posts views Thread by B-Dog | last post: by
4 posts views Thread by KarlM | last post: by
3 posts views Thread by Michael_Burgess | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.