Connecting Tech Pros Worldwide Forums | Help | Site Map

how to select next node in treeview control

praveen
Guest
 
Posts: n/a
#1: Apr 25 '06

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


AMDRIT
Guest
 
Posts: n/a
#2: Apr 25 '06

re: how to select next node in treeview control



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

"praveen" <praveen@discussions.microsoft.com> wrote in message
news:EB356B0F-E369-419A-9EB2-CAD6B45D2839@microsoft.com...[color=blue]
>
> 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
>[/color]


praveen
Guest
 
Posts: n/a
#3: Apr 25 '06

re: how to select next node in treeview control


Hi AMDRI,

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


Cheers

Praveen


"AMDRIT" wrote:
[color=blue]
>
> Me.TreeView1.SelectedNode = tnode should be selecting the node of interest
> for you. To "scroll" that node into view use tnode.ensurevisible.
>
> "praveen" <praveen@discussions.microsoft.com> wrote in message
> news:EB356B0F-E369-419A-9EB2-CAD6B45D2839@microsoft.com...[color=green]
> >
> > 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
> >[/color]
>
>
>[/color]
AMDRIT
Guest
 
Posts: n/a
#4: Apr 25 '06

re: how to select next node in treeview control


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


praveen
Guest
 
Posts: n/a
#5: Apr 26 '06

re: how to select next node in treeview control


Hi AMDRIT

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

Thanks a lot

Praveen



"AMDRIT" wrote:
[color=blue]
> 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
>
>
>[/color]
Closed Thread