with recursive function increase a pointer until you find as vbCrLf character
Try reading the text file in a string variable then split it with newline VbCrLf and then split each node with a space as you are showing in your data that each line contains a space after Id, here is an algorithmic code on similar scenario:
Dim fileText As String = System.IO.File.ReadAllText(@"your file path here")
Dim nodes As String() = fileText.Split(vbCrLf) 'you will have all nodes here
For Each item As String In nodes
Dim nodeValue As String() = item.Split(" ")
'get the node text
Dim nodeText As String = nodeValue(1)
Dim nodeId As String = nodeValue(0)
' now you can fill treeview node here
Next
नमस्ते,
[KaushaL] BlogTwitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
Good morning and thank you again for your response.
I am trying in this way, but it does not return anything ...
what am I doing wrong?
Public Sub split_string()
Form1.pippo.Nodes.Add("Schede", "Schede")
Dim fileText As String = Form1.txt_port.Text ' is a label with text
Dim nodes As String() = fileText.Split(vbCrLf) 'you will have all nodes here
For Each item As String In nodes
Dim nodeValue As String() = item.Split(" ")
'get the node text
Dim nodeText As String = nodeValue(1)
Dim nodeId As String = nodeValue(0)
' now you can fill treeview node here
Form1.pippo.Nodes(0).Nodes.Add(nodeText, nodeText, 1)
Next
End Sub
ok we had an evolution. I was making a mistake in attributing the text content. Now I have this problem:
I modified the function like this:
Public Sub split_string()
Form1.pippo.Nodes.Add("Schede", "Schede")
Dim fileText As String = Form1.txt_port.Text ' is a label with text
Dim nodes As String() = fileText.Split(vbCrLf) 'you will have all nodes here
For Each item As String In nodes
Dim nodeValue As String() = item.Split(" ")
'get the node text
Dim nodeText As String = nodeValue(0) ' i put 0 and not 1
Dim nodeId As String = nodeValue(0)
' now you can fill treeview node here
Form1.pippo.Nodes(0).Nodes.Add(nodeText, nodeText, 1)
Next
End Sub
now he fills my treeview but with blank fields.
if i use this function
Public Sub split_string()
Form1.pippo.Nodes.Add("Schede", "Schede")
Dim fileText As String = Form1.txt_port.Text ' is a label with text
Dim nodes As String() = fileText.Split(vbCrLf) 'you will have all nodes here
For Each item As String In nodes
Dim nodeValue As String() = item.Split(" ")
'get the node text
Dim nodeText As String = nodeValue(1) '
Dim nodeId As String = nodeValue(0)
' now you can fill treeview node here
Form1.pippo.Nodes(0).Nodes.Add(nodeText, nodeText, 1)
Next
End Sub
""Dim nodeText As String = nodeValue(1)"" System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
As I said earlier, I suspect there is more than one space between ID and the Node Text in the data which is why you are receiving error. Because, the code is trying to split by single space as showing below:
Dim nodeValue AsString()= item.Split(" ")
Make sure how much space is there between ID and Node Text and then apply it in split, for example for two spaces:
Dim nodeValue AsString()= item.Split(" ")
नमस्ते,
[KaushaL] BlogTwitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
kaushalparik27 Thank
you so much I solved as well as see below.
Public Sub split_string()
Form1.pippo.Nodes.Add("Schede", "Schede")
Dim fileText As String = Form1.txt_port.Text ' is a label with text
Dim nodes As String() = fileText.Split(vbCrLf) 'you will have all nodes here
For Each item As String In nodes
Dim nodeValue As String() = item.Split(vbCrLf)
'get the node text
Dim nodeText As String = nodeValue(0).ToString
Dim nodeId As String = nodeValue(0)
' now you can fill treeview node here
Form1.pippo.Nodes(0).Nodes.Add(nodeText, nodeText, 1)
Next
End Sub
None
0 Points
5 Posts
Populate a treeview with a source text variable.
Apr 10, 2017 07:51 AM|YettDar|LINK
Hello all, I need to populate a treevieew as the source but I have a text file that is made up as follows:
1 blah blah
2 blah blah
3 blah blah
4 blah blah
5 blah blah
xxxx blah blah
the numbers of elements are variable the only thing that saves me is that as I break a specific item which could be "stop-here"
I tried to work with the string variables as parameters
indexOf (number)
with recursive function increase a pointer until you find as vbCrLf character
after which I run a Mid (string, index, pointer).
I can not, however, to move to the next string. (Next line)
Can someone help me???
Thanks so much.
All-Star
15176 Points
3888 Posts
Re: Populate a treeview with a source text variable.
Apr 10, 2017 08:09 AM|raju dasa|LINK
Hi,
You don't need to use indexes and Mid()
Just split the string by newLine and then split by space (" ")
How to use split: http://stackoverflow.com/questions/14795943/how-to-split-new-line-in-string-in-vb-net
rajudasa.blogspot.com || rajudasa-tech
All-Star
31362 Points
7055 Posts
Re: Populate a treeview with a source text variable.
Apr 10, 2017 08:19 AM|kaushalparik27|LINK
Try reading the text file in a string variable then split it with newline VbCrLf and then split each node with a space as you are showing in your data that each line contains a space after Id, here is an algorithmic code on similar scenario:
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
None
0 Points
5 Posts
Re: Populate a treeview with a source text variable.
Apr 10, 2017 08:32 AM|YettDar|LINK
Good morning and thank you again for your response.
I am trying in this way, but it does not return anything ...
what am I doing wrong?
All-Star
31362 Points
7055 Posts
Re: Populate a treeview with a source text variable.
Apr 10, 2017 08:43 AM|kaushalparik27|LINK
What value you have in this TextBox? Also, please provide error/issue in detail that you are facing.
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
None
0 Points
5 Posts
Re: Populate a treeview with a source text variable.
Apr 10, 2017 08:55 AM|YettDar|LINK
in Form1.txt_port.Text i heve this: (is a label multi line)
And the out of sub
is this.
All-Star
31362 Points
7055 Posts
Re: Populate a treeview with a source text variable.
Apr 10, 2017 09:15 AM|kaushalparik27|LINK
Please debug and see what value you are receiving in this variable:
Also, I also suspect there are more than one space between ID and node values 10/100TX which also can be reason of not getting expected result.
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
None
0 Points
5 Posts
Re: Populate a treeview with a source text variable.
Apr 10, 2017 09:39 AM|YettDar|LINK
ok we had an evolution. I was making a mistake in attributing the text content. Now I have this problem:
I modified the function like this:
now he fills my treeview but with blank fields.
if i use this function
All-Star
31362 Points
7055 Posts
Re: Populate a treeview with a source text variable.
Apr 10, 2017 09:46 AM|kaushalparik27|LINK
As I said earlier, I suspect there is more than one space between ID and the Node Text in the data which is why you are receiving error. Because, the code is trying to split by single space as showing below:
Make sure how much space is there between ID and Node Text and then apply it in split, for example for two spaces:
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
None
0 Points
5 Posts
Re: Populate a treeview with a source text variable.
Apr 10, 2017 09:58 AM|YettDar|LINK