Click here to Skip to main content
15,747,059 members

Comments by vijay2482 (Top 3 by date)

vijay2482 31-Mar-11 3:43am View    
If level = 0 Then
tree_nodes(level) = trv.Nodes.Add(text_line.Trim() & level) ElseIf level = 1 Then
tree_nodes(level) = tree_nodes(level - 1).Nodes.Add(text_line.Trim & level)
'need to increment the levelfor all the sublevel with level>1
End If
vijay2482 31-Mar-11 2:05am View    
This is the below code I have to do the treeview listing.
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

'This call is required by the Windows Form Designer.

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
End If
End If
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
Me.TreeView1 = New System.Windows.Forms.TreeView
Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Fill
Me.TreeView1.ImageIndex = -1
Me.TreeView1.Location = New System.Drawing.Point(0, 0)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.SelectedImageIndex = -1
Me.TreeView1.Size = New System.Drawing.Size(292, 273)
Me.TreeView1.TabIndex = 0
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim file_name As String = Application.StartupPath
file_name = file_name.Substring(0, file_name.Length - 1)
file_name = file_name.Substring(0, file_name.LastIndexOf("\"))
file_name &= "\test.txt"
LoadTreeViewFromFile(file_name, TreeView1)
End Sub

' Load a TreeView control from a file that uses tabs
' to show indentation.
Private Sub LoadTreeViewFromFile(ByVal file_name As String, ByVal trv As TreeView)
' Get the file's contents.
Dim stream_reader As New StreamReader(file_name)
Dim file_contents As String = stream_reader.ReadToEnd()

' 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 TreeNode
Dim num_nodes As Integer = 0
ReDim tree_nodes(num_nodes)

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 - _

' 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() & level)
tree_nodes(level) = tree_nodes(level - 1).Nodes.Add(text_line.Trim() & level)
End If
vijay2482 30-Mar-11 11:25am View    
I have searched the articals already.
But did not find one, that matches my requirement.
I found a code, that does the treeview listing of a txt file, but as I mentioned, I want the levels to be written differently.

Thanks for your reply.