473,320 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 6900

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Billy Jacobs | last post by:
I have a treeview control in my application. If I right- click a node, it highlights the node but does not set it to be selected. If you move off the control it highlights the previously selected...
4
by: G Uljee | last post by:
How can I find and select an specific item in an treeview control? I want to create a search feature on my treeview. Thanks in advance, Gaby
2
by: Stephen | last post by:
In asp.net 1.1, the IE treeview web control has a property called "SelectExpands" that, when set to true, expands a node when a user clicks the node text. I can't seem to replicate this in the...
13
by: André Nogueira | last post by:
Hi there. I know you can view a node's fullpath property, but is it posible to select a node using its path? Like, tell the treeview that the node that should be selected is the node with the...
2
by: B-Dog | last post by:
I was trying to use the treeview and was wondering if there was a way to make the treeview select the node when you click the plus sign next to it. The only way I've found to do it is to actually...
4
by: KarlM | last post by:
Hallo! I'm filling my treenode tv with AD-infos with the following code: ____________________________________________________________________ private enum AdImages { AdRoot, Ou, Container,...
3
by: Michael_Burgess | last post by:
Hi there, I've looked around the different groups and still can't figure this out without resorting to scrappy code............ I want to programtically select and highlight a TreeView node,...
0
by: jiing | last post by:
Hi all, I want to use sting(the same as Node.Text) to judge if a node exists in TreeView. I've tried several ways, but seems all failed. could anybody help me? Thanks in advance. //My...
0
by: divya1949 | last post by:
Create a windows c# application which will Read a xml file and populate nodes in the treeview. 1 On selection of treenode display the child nodes of that node in listview control 2. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.