|
Sub SearchWhat(stxt As String)
On Error Resume Next
Dim i As Long, p As Long, m As Long
If TreeView.Nodes.Count = 0 Then '判断TreeView里是否有节点
MsgBox "无相关节点记录!", vbExclamation, "提示"
Exit Sub
End If
p = 1
m = TreeView.SelectedItem.Index
For i = 1 To TreeView.Nodes.Count '开始逐个查找和节点收起
TreeView.Nodes(i).Parent.Expanded = False '节点收起
' TreeView.Nodes(i).ForeColor = 0& '节点变黑(用获得焦点方式来达到变红目的,变红方式可以删除)
If i > m And p = 1 And (InStr(1, TreeView.Nodes(i).Text, UCase(stxt)) > 0 Or InStr(1, TreeView.Nodes(i).Text, LCase(stxt)) > 0) Then
'如果子项找到了
p = i
End If
Next
'TreeView.Nodes(p).ForeColor = vbRed '被查到的节点的变成红色,(用获得焦点方式来达到变红目的,变红方式可以删除)
i = p '记录当前变红节点的index号
While i > 1 '循环展开,直到第一个节点
TreeView.Nodes(i).Parent.Expanded = True '上级节点展开
i = TreeView.Nodes(i).Parent.Index '上级节点的Index
Wend
TreeView.SetFocus
TreeView.Nodes(p).Selected = True '选中的节点
Call Treeview_NodeClick(TreeView.Nodes(p)) '调用nodeclick事件的程序
If i = TreeView.Nodes.Count Then
MsgBox ("已搜索到最后")
End If
End Sub |
|