|
-
Feb 26th, 2011, 05:04 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Treenode search
I know that I can use treeview.nodes.find() but that will find the exact text so how would I search a treeview to find like items?
I have come close but this only searches the parents, never the children. Would I have to do a double search, start at parent and then get all nodes in parent?
Code:
Private Function SearchTree(ByVal sSearchString As String) As Boolean
Dim l As Integer
Dim n As TreeNode
'Dim sSearchString As String
For Each n In FrmMain.tvwMain.Nodes("RecipeBank").Nodes
'MsgBox(n.Text)
If CBool(InStr(1, n.Text, sSearchString, vbTextCompare)) Then
FrmMain.tvwMain.SelectedNode = n
'MsgBox(FrmMain.tvwMain.SelectedNode)
SearchTree = True
lLastNodeFound = l
'or call the click event : TreeView1_NodeClick n
Exit Function
End If
Next
SearchTree = False
lLastNodeFound = 0
End Function
-
Feb 26th, 2011, 06:05 PM
#2
Re: Treenode search
To search the child's, you need to make some kind of recursive function...
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Feb 26th, 2011, 06:26 PM
#3
Re: Treenode search
try this. it's a recursive search:
vb Code:
Private Sub SearchTree(ByVal startNode As TreeNode, ByVal sSearchString As String)
If startNode.Text.Contains(sSearchString) Then
startNode.ForeColor = Color.Red
Else
startNode.ForeColor = Color.Black
End If
For Each n As TreeNode In startNode.Nodes
SearchTree(n, sSearchString)
Next
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2011, 09:47 AM
#4
Thread Starter
Frenzied Member
Re: Treenode search
I thought it might be a recursive function. Was hoping there was another way.
So, doing it that way, it is possible to pause it after each one found and then have a button to continue? until it reaches end?
So search for a string, once it finds one stop search and highlight the node, instead of turning it a different color, but then have a button to continue the search from that spot on. I know I have to remember where it left off which shouldn't be a problem. but pausing it is the problem. how do you pause a recursive search lol
-
Feb 27th, 2011, 12:47 PM
#5
Re: Treenode search
use an okcancel msgbox in the function
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2011, 02:13 PM
#6
Re: Treenode search
vb Code:
Public Class Form1
Private cancelled As Boolean = False
Private Sub SearchTree(ByVal startNode As TreeNode, ByVal sSearchString As String)
If cancelled Then Return
Dim response As MsgBoxResult
If startNode.Text.Contains(sSearchString) Then
startNode.TreeView.SelectedNode.BackColor = startNode.TreeView.BackColor
startNode.TreeView.SelectedNode.ForeColor = startNode.TreeView.ForeColor
startNode.TreeView.SelectedNode = startNode
startNode.BackColor = SystemColors.Highlight
startNode.ForeColor = SystemColors.HighlightText
response = MsgBox("click ok to continue search, or cancel to abort search", MsgBoxStyle.OkCancel Or MsgBoxStyle.DefaultButton1 Or MsgBoxStyle.Question)
If response = MsgBoxResult.Cancel Then cancelled = True
End If
For Each n As TreeNode In startNode.Nodes
SearchTree(n, sSearchString)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SearchTree(TreeView1.Nodes(0), "searchText")
cancelled = False
TreeView1.SelectedNode.BackColor = TreeView1.BackColor
TreeView1.SelectedNode.ForeColor = TreeView1.ForeColor
TreeView1.Focus()
End Sub
End Class
Last edited by .paul.; Feb 27th, 2011 at 02:31 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 28th, 2011, 08:25 AM
#7
Thread Starter
Frenzied Member
Re: Treenode search
Thank you once again paul, that worked as expected.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|