Populate a Tree View from a text file.[RESOLVED]

jsumphress

Member
Joined
Sep 20, 2005
Messages
6
Programming Experience
Beginner
Is there a way to add nodes to a tree using a text file? I want my program to add the nodes on the Page Load Event. The reason I am doing this is to allow the user to add nodes, and then save them in the file.

Thanks for any help

Jason
 
Populate TreeView from text file

Ok, all you need are couple lines ... :)

VB.NET:
[size=2] 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] tvNode [/size][size=2][color=#0000ff]As[/color][/size][size=2] TreeNode
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oFile [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.IO.File
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oRead [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.IO.StreamReader
 
oRead = oFile.OpenText(Application.StartupPath & "/myText.txt")
 
tvNode = [/size][size=2][color=#0000ff]Me[/color][/size][size=2].TreeView1.Nodes.Add("something") [color=seagreen]'this is the root node[/color]
 
[/size][size=2][color=#0000ff]While[/color][/size][size=2] oRead.Peek <> -1
 
tvNode.Nodes.Add(oRead.ReadLine()) [color=seagreen]'child node/s[/color]
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]While
 
[/color][/size][size=2]oRead.Close()
[/size][size=2][color=#0000ff]
[/color][/size]


usage: just draw a treeview control on the form and double click the form. So, just join this code to the Load event ... that's it :D

Regards ;)
 

Attachments

  • TreeViewFromTextFile.zip
    21.4 KB · Views: 111
Back
Top