Greetings:
While I am not new to VB, I am new to VB.Net and I have NEVER worked with TreeView controls. Accordingly, I am in need of some (I am sure) very fundamental help.
I am working with a flat file (.txt). The following is a sample of the data format from Test.txt:
S{0001}00.00.00.00 Text Level 1
S{0002}00.01.00.00 Text Level 2
S{0003}00.01.01.00 Text Level 3
S{0003}00.01.05.00 Text Level 3
S{0003}00.01.05.01 Text Level 4
S{0002}00.10.00.00 Text Level 2
S{0002}00.11.00.00 Text Level 2
S{0003}00.11.13.00 Text Level 3
S{0003}00.11.16.00 Text Level 3
S{0001}01.00.00.00 Text Level 1
S{0002}01.10.00.00 Text Level 2
S{0002}01.11.00.00 Text Level 2
S{0003}01.11.13.00 Text Level 3
S{0003}01.11.13.01 Text Level 4
S{0003}01.11.13.05 Text Level 4
Below is how I suppose TreeView1 should appear populated from the above data:
00.00.00.00 Text Level 1
00.01.00.00 Text Level 2
00.01.01.00 Text Level 3
00.01.05.00 Text Level 3
00.01.05.01 Text Level 4
00.10.00.00 Text Level 2
00.11.00.00 Text Level 2
00.11.13.00 Text Level 3
00.11.16.00 Text Level 3
01.00.00.00 Text Level 1
01.10.00.00 Text Level 2
01.11.00.00 Text Level 2
01.11.13.00 Text Level 3
01.11.13.01 Text Level 4
01.11.13.05 Text Level 4
The S at the start of each line (designating Static) is destined for the Tag property for its relative node and the {nnnn} denotes the node level. The remaining portion of the string (line) is destined to be the nodes Text (i.e. 00.10.00.00 Text Level 2).
I know very well that attempt at coding this is porly done and incorrect. I need help at making this work:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim line As String, TempLine As String, PrimeLevel As Integer, Level1 As Integer, _
Level2 As Integer, Level3 As Integer, Level4 As Integer, CurLev As Integer
TreeView1.Nodes.Clear()
TreeView1.Nodes.Add("MasterFormat 2004")
PrimeLevel = 0
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("C:\Users\lhasha\Documents\Test.txt")
' Read and display the lines from the file until the end
' of the file is reached.
'TreeView1.Nodes.Clear()
Level1 = -1
Level2 = -1
Level3 = -1
Level4 = -1
Do
line = sr.ReadLine()
TempLine = Mid(line, 8)
CurLev = Val(Mid(line, 3, 4))
If CurLev = 1 Then 'LEVEL 1
Level1 = Level1 + 1
Level2 = -1
Level3 = -1
Level4 = -1
TreeView1.Nodes(PrimeLevel).Nodes.Add(Level1, TempLine)
GoTo Out
End If
If CurLev = 2 Then 'LEVEL 2
Level2 = Level2 + 1
TreeView1.Nodes(PrimeLevel).Nodes(Level1).Nodes.Add(TempLine)
GoTo Out
End If
If CurLev = 3 Then 'LEVEL 3
Level3 = Level3 + 1
TreeView1.Nodes(PrimeLevel).Nodes(Level1).Nodes(0).Nodes.Add(TempLine)
GoTo Out
End If
If CurLev = 4 Then 'LEVEL 4
TreeView1.Nodes(PrimeLevel).Nodes(Level1).Nodes(0).Nodes(0).Nodes.Add(TempLine)
GoTo Out
End If
Out:
Loop Until line Is Nothing
sr.Close()
Catch Q As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(Q.Message)
End Try
End Sub
THANK YOU!
While I am not new to VB, I am new to VB.Net and I have NEVER worked with TreeView controls. Accordingly, I am in need of some (I am sure) very fundamental help.
I am working with a flat file (.txt). The following is a sample of the data format from Test.txt:
S{0001}00.00.00.00 Text Level 1
S{0002}00.01.00.00 Text Level 2
S{0003}00.01.01.00 Text Level 3
S{0003}00.01.05.00 Text Level 3
S{0003}00.01.05.01 Text Level 4
S{0002}00.10.00.00 Text Level 2
S{0002}00.11.00.00 Text Level 2
S{0003}00.11.13.00 Text Level 3
S{0003}00.11.16.00 Text Level 3
S{0001}01.00.00.00 Text Level 1
S{0002}01.10.00.00 Text Level 2
S{0002}01.11.00.00 Text Level 2
S{0003}01.11.13.00 Text Level 3
S{0003}01.11.13.01 Text Level 4
S{0003}01.11.13.05 Text Level 4
Below is how I suppose TreeView1 should appear populated from the above data:
00.00.00.00 Text Level 1
00.01.00.00 Text Level 2
00.01.01.00 Text Level 3
00.01.05.00 Text Level 3
00.01.05.01 Text Level 4
00.10.00.00 Text Level 2
00.11.00.00 Text Level 2
00.11.13.00 Text Level 3
00.11.16.00 Text Level 3
01.00.00.00 Text Level 1
01.10.00.00 Text Level 2
01.11.00.00 Text Level 2
01.11.13.00 Text Level 3
01.11.13.01 Text Level 4
01.11.13.05 Text Level 4
The S at the start of each line (designating Static) is destined for the Tag property for its relative node and the {nnnn} denotes the node level. The remaining portion of the string (line) is destined to be the nodes Text (i.e. 00.10.00.00 Text Level 2).
I know very well that attempt at coding this is porly done and incorrect. I need help at making this work:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim line As String, TempLine As String, PrimeLevel As Integer, Level1 As Integer, _
Level2 As Integer, Level3 As Integer, Level4 As Integer, CurLev As Integer
TreeView1.Nodes.Clear()
TreeView1.Nodes.Add("MasterFormat 2004")
PrimeLevel = 0
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("C:\Users\lhasha\Documents\Test.txt")
' Read and display the lines from the file until the end
' of the file is reached.
'TreeView1.Nodes.Clear()
Level1 = -1
Level2 = -1
Level3 = -1
Level4 = -1
Do
line = sr.ReadLine()
TempLine = Mid(line, 8)
CurLev = Val(Mid(line, 3, 4))
If CurLev = 1 Then 'LEVEL 1
Level1 = Level1 + 1
Level2 = -1
Level3 = -1
Level4 = -1
TreeView1.Nodes(PrimeLevel).Nodes.Add(Level1, TempLine)
GoTo Out
End If
If CurLev = 2 Then 'LEVEL 2
Level2 = Level2 + 1
TreeView1.Nodes(PrimeLevel).Nodes(Level1).Nodes.Add(TempLine)
GoTo Out
End If
If CurLev = 3 Then 'LEVEL 3
Level3 = Level3 + 1
TreeView1.Nodes(PrimeLevel).Nodes(Level1).Nodes(0).Nodes.Add(TempLine)
GoTo Out
End If
If CurLev = 4 Then 'LEVEL 4
TreeView1.Nodes(PrimeLevel).Nodes(Level1).Nodes(0).Nodes(0).Nodes.Add(TempLine)
GoTo Out
End If
Out:
Loop Until line Is Nothing
sr.Close()
Catch Q As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(Q.Message)
End Try
End Sub
THANK YOU!