如何在特定深度搜索多个树视图节点
How to search multiple treeview nodes at a particular depth
请问有谁知道如何通过单击按钮在特定深度搜索多个树视图节点的文本?树视图节点排列如下:
我想防止用户将相同标题的重复孙节点输入到树视图中,即第二次输入 'Movie 2' 应该会抛出一条消息,表明已经输入了电影 2;如果没有,则添加新的电影标题。
孙节点标题从文本框输入到树视图中。我正在使用 Visual Basic 2010 Express。提前谢谢你。
我使用的代码是:
Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click
'New movie title has been introduced into the study
Dim SelectedNode As TreeNode
SelectedNode = TreeView1.SelectedNode
'To avoid entering duplicate movies title
Dim NewMovieName As String = TextBox1.Text.Trim.ToLower ' The content of that node
Dim parentNode = SelectedNode.Parent ' Get the parent
Dim childNodes As TreeNodeCollection = parentNode.Nodes ' Get all the children
Dim WeHaveDuplicate As Boolean = False ' We use this to flag if a duplicate is found. Initially set to false.
For Each tempNode As TreeNode In childNodes
'Test that we have the same name but not referring to the same node
If tempNode.Text.Trim.ToLower = NewMovieName And tempNode IsNot parentNode Then WeHaveDuplicate = True
Next
If WeHaveDuplicate = True Then
'Send message to user
MsgBox(TextBox1.Text & " as a parameter has already been considered.", vbOKOnly)
Exit Sub
Else
parentNode.Nodes.Add(TextBox1.Text)
TreeView1.ExpandAll()
End If
Exit Sub
End Sub
所有帮助将不胜感激。谢谢。
这是我经常使用的一个小片段。它将通过它的文本找到一个节点。它还会突出显示并展开找到的节点。
注意它是递归的,因此它将搜索到提供的节点集合(参数)的底部。如果这个提供的集合是根节点,那么它将搜索整棵树。
我通常将一个唯一的字符串应用于 node.tag 属性。如果您调整函数来查找那个,您可以在显示重复文本的同时仍然有一个唯一的字符串来查找...
''' <summary>
''' Find and Expand Node in Tree View
''' </summary>
Private Function FindNode(ByVal SearchText As String, ByVal NodesToSearch As TreeNodeCollection, ByVal TreeToSearch As TreeView) As TreeNode
Dim ReturnNode As TreeNode = Nothing
Try
For Each Node As TreeNode In NodesToSearch
If String.Compare(Node.Text, SearchText, True) = 0 Then
TreeToSearch.SelectedNode = Node
Node.Expand()
ReturnNode = Node
Exit For
End If
If ReturnNode Is Nothing Then ReturnNode = FindNode(SearchText, Node.Nodes, TreeToSearch)
Next
Catch ex As Exception
Throw
End Try
Return ReturnNode
End Function
已编辑:
根据您最近的评论,
您可以尝试像这样使用它...
WeHaveDuplicate = (FindNode("Movie 2", TreeView1.Nodes, TreeView1) Is Nothing)
If WeHaveDuplicate = True Then
'message user of dupe
Else
'add movie
End If
请问有谁知道如何通过单击按钮在特定深度搜索多个树视图节点的文本?树视图节点排列如下:
我想防止用户将相同标题的重复孙节点输入到树视图中,即第二次输入 'Movie 2' 应该会抛出一条消息,表明已经输入了电影 2;如果没有,则添加新的电影标题。
孙节点标题从文本框输入到树视图中。我正在使用 Visual Basic 2010 Express。提前谢谢你。
我使用的代码是:
Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click
'New movie title has been introduced into the study
Dim SelectedNode As TreeNode
SelectedNode = TreeView1.SelectedNode
'To avoid entering duplicate movies title
Dim NewMovieName As String = TextBox1.Text.Trim.ToLower ' The content of that node
Dim parentNode = SelectedNode.Parent ' Get the parent
Dim childNodes As TreeNodeCollection = parentNode.Nodes ' Get all the children
Dim WeHaveDuplicate As Boolean = False ' We use this to flag if a duplicate is found. Initially set to false.
For Each tempNode As TreeNode In childNodes
'Test that we have the same name but not referring to the same node
If tempNode.Text.Trim.ToLower = NewMovieName And tempNode IsNot parentNode Then WeHaveDuplicate = True
Next
If WeHaveDuplicate = True Then
'Send message to user
MsgBox(TextBox1.Text & " as a parameter has already been considered.", vbOKOnly)
Exit Sub
Else
parentNode.Nodes.Add(TextBox1.Text)
TreeView1.ExpandAll()
End If
Exit Sub
End Sub
所有帮助将不胜感激。谢谢。
这是我经常使用的一个小片段。它将通过它的文本找到一个节点。它还会突出显示并展开找到的节点。
注意它是递归的,因此它将搜索到提供的节点集合(参数)的底部。如果这个提供的集合是根节点,那么它将搜索整棵树。
我通常将一个唯一的字符串应用于 node.tag 属性。如果您调整函数来查找那个,您可以在显示重复文本的同时仍然有一个唯一的字符串来查找...
''' <summary>
''' Find and Expand Node in Tree View
''' </summary>
Private Function FindNode(ByVal SearchText As String, ByVal NodesToSearch As TreeNodeCollection, ByVal TreeToSearch As TreeView) As TreeNode
Dim ReturnNode As TreeNode = Nothing
Try
For Each Node As TreeNode In NodesToSearch
If String.Compare(Node.Text, SearchText, True) = 0 Then
TreeToSearch.SelectedNode = Node
Node.Expand()
ReturnNode = Node
Exit For
End If
If ReturnNode Is Nothing Then ReturnNode = FindNode(SearchText, Node.Nodes, TreeToSearch)
Next
Catch ex As Exception
Throw
End Try
Return ReturnNode
End Function
已编辑:
根据您最近的评论,
您可以尝试像这样使用它...
WeHaveDuplicate = (FindNode("Movie 2", TreeView1.Nodes, TreeView1) Is Nothing)
If WeHaveDuplicate = True Then
'message user of dupe
Else
'add movie
End If