我做了一个小时钟计划,我使用树视图列出员工及其活动(只有1级深度)。
我正在寻找一种方法,可以根据与他们名字的第一个字母相对应的字母跳转到下一位员工。我找不到任何东西。
这就是我想出来的。
Private Sub TryToJump(strLetter As String)
Dim intSelectedNode As Integer
Dim bolMatchFound As Boolean
If tvEmployees.Nodes.Count = 0 Then Exit Sub 'There are no nodes to search through so exit the sub
Dim tnTemp As TreeNode = tvEmployees.SelectedNode 'See if any node is selected
If tnTemp Is Nothing Then
intSelectedNode = 0 'No node was selected so start at 0 when searching
Else
intSelectedNode = tnTemp.Index + 1 'A node is selected so start the search at the node after the selected node
If intSelectedNode >= tvEmployees.Nodes.Count Then 'If the selected node is the end of the line
intSelectedNode = 0 'Start back at the beginning
End If
End If
bolMatchFound = False 'Set a flag to determine if an entry was found or not
For i As Integer = intSelectedNode To tvEmployees.Nodes.Count - 1 'go through each node starting at the first entry past the currently selected (or 0 if none are selected)
If tvEmployees.Nodes(i).Parent Is Nothing Then 'This is a parent node so check the first letter
If UCase(Strings.Left(tvEmployees.Nodes(i).Text, 1)) = UCase(strLetter) Then 'If the first letter matches
tvEmployees.SelectedNode = tvEmployees.Nodes(i) 'Select that parent node
bolMatchFound = True 'Set the flag that the next match in line was found
Exit For 'And exit the loop
End If
End If
Next
If Not bolMatchFound Then 'If we got to the end and there was not a match found
For i As Integer = 0 To intSelectedNode 'Go through from the beginning up to the previously selected node
If tvEmployees.Nodes(i).Parent Is Nothing Then 'If it is a parent
If UCase(Strings.Left(tvEmployees.Nodes(i).Text, 1)) = UCase(strLetter) Then 'Check if the first letter matches
tvEmployees.SelectedNode = tvEmployees.Nodes(i) 'Select it if it does
Exit For 'And exit
End If
End If
Next
End If
End Sub
我大部分都是自学成才,这很有用但我很好奇一些经验丰富的程序员会采取不同的方法。
这会遍历所有节点(我的列表不是很大,所以它不应该是速度问题)并检查每个父节点的第一个字母,如果匹配则选择父节点。
如果它到达终点,它会在开始时返回,直到它到达最初选择的节点。因此,用户可以继续按“M”来获取他们的名字。
然后,用户可以习惯他们需要多少次按下才能获得时间 同样,它有效,但我很好奇其他人如何处理这个问题。感谢。
答案 0 :(得分:1)
您可以使用Linq找到项目文本TreeNode
的第一个.Where()
.StartsWith()
搜索模式和TreeNode.Level = 0
。
然后,使用TreeNode.EnsureVisible()
使其可见。
您可能还希望.SelectedNode = [FoundNode]
突出显示它。
这里我使用TreeView KeyUp()
事件来触发搜索,但当然可以从其他任何地方调用FindNode()
方法,前提是你可以传递一个有意义的字符串。 />
更改搜索模式时,会创建一个新的TreeNodes选项
如果模式没有改变,则重用,因此您不需要解析整个TreeView节点集合
按相同的键将循环选择TreeNodes。
Private CurrentNodeIndex As Integer = 0
Private TreeNodeSearchString As String = String.Empty
Private TreeNodeSelection As IEnumerable(Of TreeNode)
Private Sub tvEmployees_KeyUp(sender As Object, e As KeyEventArgs) Handles tvEmployees.KeyUp
FindNode(Chr(e.KeyValue))
End Sub
Private Sub FindNode(sPattern As String)
If sPattern <> TreeNodeSearchString Then
TreeNodeSelection = tvEmployees.Nodes.
Cast(Of TreeNode)().
Where(Function(tn) (tn.Text.StartsWith(sPattern) AndAlso tn.Level = 0))
CurrentNodeIndex = 0
TreeNodeSearchString = sPattern
End If
If TreeNodeSelection.Count > 0 Then
TreeNodeSelection(CurrentNodeIndex).EnsureVisible()
tvEmployees.SelectedNode = TreeNodeSelection(CurrentNodeIndex)
CurrentNodeIndex += 1
If CurrentNodeIndex >= TreeNodeSelection.Count Then
CurrentNodeIndex = 0
End If
End If
End Sub