Click here to Skip to main content
15,887,417 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

The General TreeView Structure, with the level indicated in the left.

0 01603301
1 4357848M91
2 4277562M91
3 4277563M1
3 1441447X1
3 4277564M91
1 4357848M91
2 4277562M91
3 4277563M1
3 1441447X1
3 4277564M91
4 1441447X1
1 4357848M91
1 4355777M91
2 1441447X1
2 1441447X1
1 1441447X1
1 1441447X1

I need the level as follows instead of the general Treeview Level.

0 01603301
1 4357848M91
2 4277562M91
3 4277563M1
3 1441447X1
3 4277564M91
1 4357848M91
4 4277562M91
5 4277563M1
5 1441447X1
5 4277564M91
6 1441447X1
1 4357848M91
1 4355777M91
7 1441447X1
7 1441447X1
1 1441447X1
1 1441447X1

Leaving the 0 and 1 levels, the other levels has to be treated as above.
How to achieve this?
Any suggetion and help will be helpful.
Thanks in advance.
Posted
Comments
Marc A. Brown 30-Mar-11 12:18pm    
I moved your reply to digital man's solution so that it's a comment to his solution instead of a solution of its own. Otherwise he wouldn't have been likely to see it.

If only there was a place you could search for articles[^] on TreeViews...
 
Share this answer
 
Comments
Marc A. Brown 30-Mar-11 12:17pm    
From the OP: 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.
Sergey Alexandrovich Kryukov 30-Mar-11 13:44pm    
@vijay2482: did you really expect to find exact code sample for your (not very reasonable) requirements?
You're supposed to code this, not to find a solution.
Also, please understand: who will be interested to write any code on you spec? CodeProject does not work this way.

Finally, it's only useful for you if you write it yourself. If you face some problems -- ask a Question. (Please, don't post it as Answer anymore.)

--SA
This is the 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()
        MyBase.New()
        'This call is required by the Windows Form Designer.
        InitializeComponent()
        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'TreeView1
        '
        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
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.TreeView1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    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()
        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 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(charTab).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)
                Else
                    tree_nodes(level) = tree_nodes(level - 1).Nodes.Add(text_line.Trim() & level)
                End If
                tree_nodes(level).EnsureVisible()
            End If
        Next i
        If trv.Nodes.Count > 0 Then trv.Nodes(0).EnsureVisible()
    End Sub
End Class


Leaving the 0 and 1 levels, the other levels has to be incremented.
Any suggestions will be helpful.
Thanks in advance.
 
Share this answer
 
v2
Comments
R. Giskard Reventlov 31-Mar-11 2:32am    
READ THE ARTICLES: I'm not wading through your code trying to decide what, if any, problems it has. Where, exactly, is it going wrong? What have you tried to do to fix it yourself?
vijay2482 31-Mar-11 3:43am    
Code:
<Pre>
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)
Else
'need to increment the levelfor all the sublevel with level>1
End If
<Pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900