'***************************************************************
' Name: Find Treeview Node
' Description:Allows users to search through top level of a treev
' iew for a value just like the find in a text editor.
If value is found node is automatically selected. Could be re-coded to search a whole tree including child nodes.
Form and code supplied.
' By: Stefan Garnham
'
'
' Inputs:None
'
' Returns:None
'
'Assumes:None
'
'Side Effects:Should be OK to run on any platform but only tested
' on VB 5.o
'
'Code provided by Planet Source Code(tm) (http://www.PlanetSource
' Code.com) 'as is', without warranties as to performance, fitness,
' merchantability,and any other warranty (whether expressed or impl
' ied).
'***************************************************************
VERSION 5.00
Begin VB.Form frmFind
BorderStyle =3 'Fixed Dialog
Caption ="Find"
ClientHeight=1485
ClientLeft =45
ClientTop=330
ClientWidth =4035
Icon="frmFind.frx":0000
LinkTopic="Form1"
MaxButton=0'False
MinButton=0'False
ScaleHeight =1485
ScaleWidth =4035
ShowInTaskbar=0'False
StartUpPosition =3 'Windows Default
Begin VB.CheckBox chkWhole
Caption ="&Match whole entry"
Height =255
Left=240
TabIndex=2
Top =1080
Width=2175
End
Begin VB.CommandButton cmdClose
Cancel =-1 'True
Caption ="&Close"
Height =375
Left=2760
TabIndex=4
Top =960
Width=1095
End
Begin VB.CommandButton cmdFind
Caption ="&Find"
Default =-1 'True
Height =375
Left=2760
TabIndex=3
Top =420
Width=1095
End
Begin VB.TextBox txtFind
Height =375
Left=240
TabIndex=1
Top =540
Width=2235
End
Begin VB.Label Label1
Caption ="&Text to look For ..."
Height =255
Left=240
TabIndex=0
Top =240
Width=2235
End
End
Attribute VB_Name = "frmFind"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Public tvwSearch As TreeView
Dim iStart%
Dim iPass%
Private Sub cmdClose_Click()
Set tvwSearch = Nothing
Unload Me
End Sub
Private Sub cmdFind_Click()
Dim strToFind$, iIndex%
Dim blnFound As Boolean
strToFind = LCase$(txtFind)
With tvwSearch
'check not on child node and an item selected
If .SelectedItem Is Nothing Then
iStart = 1
.SelectedItem = .Nodes(iStart)
ElseIf Not .SelectedItem.Parent Is Nothing Then
iStart = .SelectedItem.Parent.Index
Else
iStart = .Nodes(.SelectedItem.Index).Next.Index
End If
LOOKAGAIN:
Screen.MousePointer = vbHourglass
iIndex = iStart
'loop through nodes looking for the text
Do
'check for matching whole entry
If chkWhole Then
'match all of entry
If .Nodes(iIndex) = strToFind Then blnFound = True
Else
'match part entry
If LCase$(.Nodes(iIndex)) Like "*" & strToFind & "*" Then blnFound = True
End If
If blnFound Then
.SelectedItem = .Nodes(iIndex)
.Nodes(.SelectedItem.Index).EnsureVisible
cmdFind.Caption = "&Find Next"
Screen.MousePointer = vbDefault
Exit Sub
End If
iIndex = .Nodes(iIndex).Next.Index
Loop While Not iIndex = .Nodes(iStart).LastSibling.Index
Screen.MousePointer = vbDefault
'If we started lower down the tree
'and haven't found the string
If Not blnFound And iPass = 0 Then
'see if user wants to search
'from top of tree
If MsgBox(strToFind & " was not found." & vbCrLf & "Search from beginning?", _
vbQuestion + vbYesNo, "Not Found") = vbNo Then
.SelectedItem = .Nodes(iStart)
Unload Me
Exit Sub
End If
'select first node and search again
iStart = 1
iPass = 1
Goto LOOKAGAIN
Else
'notify user that text not found
MsgBox strToFind & " was not found.", vbInformation, "Not Found"
cmdFind.Caption = "&Find"
iPass = 0
iStart = 0
txtFind.SetFocus
txtFind.SelStart = 0
txtFind.SelLength = Len(txtFind)
End If
End With
End Sub
Private Sub Form_Load()
iStart = 0
iPass = 0
End Sub
Private Sub txtFind_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyDelete Or KeyAscii = vbKeyBack Then
cmdFind.Caption = "&Find"
iPass = 0
iStart = 1
End If
End Sub