Usuário com melhor resposta
Pesquisar, pelo nome, e selecionar um node no TreeView via código

Pergunta
-
Estou montando um sistema parecido com o Windows Explorer utilizando o TreeView e o WebBrowser.
Estou usando o seguinte função para pesquisar o node no TreeView
Private Function GetNode(ByVal text As String, ByVal parentCollection As TreeNodeCollection) As TreeNode
Dim ret As TreeNode
Dim child As TreeNode
For Each child In parentCollection 'step through the parentcollection
If child.Text = text Then
ret = child
ElseIf child.GetNodeCount(True) > 0 Then ' if there is child items then call this function recursively
ret = GetNode(text, child.Nodes)
End If
If Not ret Is Nothing Then Exit For 'if something was found, exit out of the for loop
Next
Return ret
End Functione o seguinte código para acioná-la e selecionar o node
Private Sub WebBrowser1_Navigated(sender As System.Object, e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated Dim vetor() As String vetor = Split(WebBrowser1.Url.ToString.Substring(8), "/") TreeView1.SelectedNode = GetNode(vetor(vetor.Length - 1), TreeView1.Nodes) TreeView1.Select() End Sub
A função funciona quase perfeitamente, eu entro com o nome do node e ele e selecionado*, porém se tiver mais de um node com o nome igual ele retorna o node errado.
Ex.: Tendo a seguinte TreeView:
-Documents
--RAD Studio
---Projects
--Visual Studio 2010
---Projects
Através do WebBrowser navego até a pasta Documents\Visual Studio 2010 - o TreeView seleciona:
-Documents
--RAD Studio
---Projects
--Visual Studio 2010 *****
---Projects
Ao navegar a pasta Documents\Visual Studio 2010\Projects ele seleciona
-Documents
--RAD Studio
---Projects*****
--Visual Studio 2010
---Projects
O correto seria:
-Documents
--RAD Studio
---Projects
--Visual Studio 2010
---Projects*****
Ao navegar a pasta Documents\RAD Studio\Projects ele seleciona
-Documents
--RAD Studio
---Projects*****
--Visual Studio 2010
---Projects
Como corrigir esse erro?
- Editado Osvaldo1br sexta-feira, 14 de setembro de 2012 14:55
Respostas
-
graça a sua ajuda eu descobri onde esta o erro do meu código, eu estava pegando o caminho desta maneira "C:/Users/teste/Documents/Visual Studio 2010/Project" (que seria a url do WebBrowser1) e o certo era "Documents\Visual Studio 2010\Project", troca feita através do path.Replace("/","\") e a troca do WebBrowser1.Url.ToString.Substring(8) por WebBrowser1.Url.ToString.Substring(34), e também troquei o child.Text por child.FullPath, ai funcionou corretamente.
Ficando assim:
Private Function GetNode(ByVal path As String, ByVal parentCollection As TreeNodeCollection) As TreeNode Dim ret As TreeNode For Each child As TreeNode In parentCollection 'step through the parentcollection If child.FullPath = path.Replace("/", "\") Then ret = child ElseIf child.GetNodeCount(True) > 0 Then ' if there is child items then call this function recursively ret = GetNode(path.Replace("/", "\"), child.Nodes) End If If Not ret Is Nothing Then Exit For 'if something was found, exit out of the for loop Next Return ret End Function
Private Sub WebBrowser1_Navigated(sender As System.Object, e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated TreeView1.SelectedNode = GetNode(WebBrowser1.Url.ToString.Substring(34), TreeView1.Nodes) TreeView1.Select() End Sub
- Editado Osvaldo1br domingo, 16 de setembro de 2012 19:34
- Sugerido como Resposta Jeimes Ribeiro segunda-feira, 17 de setembro de 2012 12:12
- Marcado como Resposta Harley AraujoModerator terça-feira, 18 de setembro de 2012 12:44
Todas as Respostas
-
Olá,
Vou tentar explicar a causa disto. Temos o TreeView da seguinte maneira:
-Documents
--RAD Studio
---Projects
--Visual Studio 2010
---Projects
Na sua função GetNode é executado um laço for each pela coleção do treeview. Acontece que o laço está procurando o Node correto de forma Unidimensional, isto é, sem levar em conta o node pai. O For Each está vasculhando a coleção como se fosse uma lista assim:
Documents
RAD Studio
Projects
Visual Studio 2010
Projects
Ou seja ele está retornando o primeiro resultado que encontra, sempre!
Você pode experimentar algo assim:
Private Function GetNode(ByVal path As String, ByVal parentCollection As TreeNodeCollection) As TreeNode Dim ret As TreeNode For Each child As TreeNode In parentCollection 'step through the parentcollection If child.FullPath = Path.Replace("/","\") Then ret = child End If If Not ret Is Nothing Then Exit For 'if something was found, exit out of the for loop Next Return ret End Function Private Sub WebBrowser1_Navigated(sender As System.Object, e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated TreeView1.SelectedNode = GetNode(WebBrowser1.Url.ToString.Substring(8), TreeView1.Nodes) TreeView1.Select() End Sub
Não esqueça de alterar a propriedade PathSeparator do seu TreeView para "\" :
TreeView1.PathSeparator = "\"
Visual Studio 2010(Vb.Net)
- Editado Herbert Lausmann sábado, 15 de setembro de 2012 18:51 Esqueci de uma coisa....
- Sugerido como Resposta Jeimes Ribeiro segunda-feira, 17 de setembro de 2012 12:12
-
graça a sua ajuda eu descobri onde esta o erro do meu código, eu estava pegando o caminho desta maneira "C:/Users/teste/Documents/Visual Studio 2010/Project" (que seria a url do WebBrowser1) e o certo era "Documents\Visual Studio 2010\Project", troca feita através do path.Replace("/","\") e a troca do WebBrowser1.Url.ToString.Substring(8) por WebBrowser1.Url.ToString.Substring(34), e também troquei o child.Text por child.FullPath, ai funcionou corretamente.
Ficando assim:
Private Function GetNode(ByVal path As String, ByVal parentCollection As TreeNodeCollection) As TreeNode Dim ret As TreeNode For Each child As TreeNode In parentCollection 'step through the parentcollection If child.FullPath = path.Replace("/", "\") Then ret = child ElseIf child.GetNodeCount(True) > 0 Then ' if there is child items then call this function recursively ret = GetNode(path.Replace("/", "\"), child.Nodes) End If If Not ret Is Nothing Then Exit For 'if something was found, exit out of the for loop Next Return ret End Function
Private Sub WebBrowser1_Navigated(sender As System.Object, e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated TreeView1.SelectedNode = GetNode(WebBrowser1.Url.ToString.Substring(34), TreeView1.Nodes) TreeView1.Select() End Sub
- Editado Osvaldo1br domingo, 16 de setembro de 2012 19:34
- Sugerido como Resposta Jeimes Ribeiro segunda-feira, 17 de setembro de 2012 12:12
- Marcado como Resposta Harley AraujoModerator terça-feira, 18 de setembro de 2012 12:44