The following line is creating the above error message.
tree_nodes((level)-1).Nodes.Add(tree_nodes(level))
It's because of the following line where I'm trying to calculate the length of
blank spaces and substracting from total length.
level = text_line.Length - text_line.TrimStart.Length
Here's the code. The first part of the "IF" is fine.
Dim stream_reader As New System.IO.StreamReader(file_name)
Dim file_contents As String = stream_reader.ReadToEnd()
stream_reader.Close()
' Remove line feeds.
file_contents = file_contents.Replace(vbLf, "")
' Break the file into lines.
Const charCR As Char = CChar(vbCr)
'Const charTab As Char = CChar(vbTab)
Dim lines() As String = file_contents.Split(charCR)
' Process the lines.
Dim text_line As String
Dim level As Integer
Dim tree_nodes() As Microsoft.Web.UI.WebControls.TreeNode
Dim num_nodes As Integer = 0
ReDim tree_nodes(num_nodes)
trv.Nodes.Clear()
For i As Integer = 0 To lines.GetUpperBound(0)
text_line = lines(i)
If text_line.Trim().Length > 0 Then
' See how many tabs are at the start of the line.
level = text_line.Length - text_line.TrimStart.Length
' Make room for the new node.
If level > num_nodes Then
num_nodes = level
ReDim Preserve tree_nodes(num_nodes)
End If
' Add the new node.
If level = 0 Then
'tree_nodes(level) = trv.Nodes.Add(text_line.Trim())
tree_nodes(level) = New TreeNode
tree_nodes(level).Text = CStr(text_line.Trim())
trv.Nodes.Add(tree_nodes(level))
Else
' tree_nodes(level) = tree_nodes(level -
1).Nodes.Add(text_line.Trim())
tree_nodes(level) = New TreeNode
tree_nodes(level).Text = CStr(text_line.Trim())
tree_nodes((level)-1).Nodes.Add(tree_nodes(level))
End If
I'm reading from a file that has indentation based on spaces. If it was a
true tab-delimited file I would be okay. So, it's the blank spaces that
would determine order of the nodes. Thanks in advance.
--
Hutty