[RESOLVED] Complication with Treeview
Hello,
I am severely confused while trying to do some thing like this with treeview. Need help please... I am trying to populate an treeview by reading lines from an text file.
The text file contains the paths of the in the format below
[TOPROOT]
[TOPROOT\NULL]
[TOPROOT\NULL]
[TOPROOT\NULL\COMMONDT]
[TOPROOT\EVENT]
[TOPROOT\EVENT\ROTO]
[TOPROOT\EVENT\ROTO\CLSID0001]
[TOPROOT\EVENT\ROTO\CLSID0001\CLSID_ID]
I am trying to make the treeview using the paths as above with the top node as TOPROOT and the child node as NULL. When the program I have written reads the third line which is repeated again the program quits with an error "key not unique". Unfortunately I cannot prevent the lines in the text file from duplicating as this output is generated by another program.
It would be great if any one can give me some logic as to create the tree structure reading the paths mentioned in the text file.
Thanks in advance...
The code i am trying till now...
The Code Till Now Code:
Dim Filenum As Integer
Dim Temp As String
Dim Arr() As String
Dim x As Long
Dim k As Integer
Dim nodx As Node
Filenum = FreeFile
Open App.Path & "\test.txt" For Input As #Filenum
Do Until EOF(Filenum) = True
Line Input #Filenum, Temp
If Left$(Temp, 1) = "[" Then
Arr() = Split(Replace(Replace(Temp, "[", vbNullString), "]", vbNullString), "\")
If UBound(Arr) = 0 Then
Set nodx = Tv1.Nodes.Add(, , Arr(0), Arr(0))
nodx.Expanded = True
Else
For k = 0 To UBound(Arr)
If k > 0 Then
Set nodx = Tv1.Nodes.Add(Arr(k - 1), tvwChild, Arr(k), Arr(k))
'Else
' Set nodx = Tv1.Nodes.Add(Arr(0), tvwChild, Arr(k), Arr(k))
End If
Next
End If
x = x + 1
End If
Loop
Close #Filenum
-Putta
Re: Complication with Treeview
Store the line of text in the Tag (nodx.Tag = Arr(k)) property of the Node, and create your own Key by appending a number to it.
For instance, Dim lCnt as Long and the after each added Node,increment it by 1, so Key becomes Arr(k) & lCnt.
That should solve the problem of "Key not unique". To access the Key you really want, use nodx.Tag and not nodx.Key.
EDIT:
Make the Key the concatenation of the number and the Key. For example, use lCnt & Chr(0) & Arr(k) for the Key. Split by NullChar (Chr(0)) and Index 1 of the Array would be the Key.
Re: Complication with Treeview
Perhaps this trick of MartinLiss would be of help.
Re: Complication with Treeview
Interesting, but I wouldn't go with Random numbers. Aside from miniscule possibility, Longs are faster with just the addition of 1, which eliminates possibility.
1 Attachment(s)
Re: Complication with Treeview
Ok this works for me...
But before you do...amend your text file as follows...
TOPROOT
TOPROOT\NULL
TOPROOT\NULL\COMMONDT
TOPROOT\EVENT
TOPROOT\EVENT\ROTO
TOPROOT\EVENT\ROTO\CLSID0001
TOPROOT\EVENT\ROTO\CLSID0001\CLSID_ID
and then try this... i tried it and it works...
vb Code:
Option Explicit
Private Declare Function CoCreateGuid Lib _
"ole32.dll" (pGUID As Any) As Long
Private iFreeFile As Integer
Private Sub Command1_Click()
'~~> Change file name as applicable
LoadNodesFromFileWithFullPath "C:\Temp\1.txt"
End Sub
Private Sub LoadNodesFromFileWithFullPath(Flnm As String)
Dim text_line As String, level As Integer
Dim tree_nodes() As Node, num_nodes As Integer, pos As Long
iFreeFile = FreeFile()
Open Flnm For Input As #iFreeFile
TView.Nodes.Clear
Do While Not EOF(iFreeFile)
Line Input #iFreeFile, text_line
level = UBound(Split(text_line, "\")) + 1
If level > num_nodes Then
num_nodes = level
ReDim Preserve tree_nodes(1 To num_nodes)
End If
pos = InStrRev(text_line, "\")
If pos Then text_line = Mid$(text_line, pos + 1)
If level = 1 Then
Set tree_nodes(level) = TView.Nodes.Add(, , CreateGUID, text_line)
Else
Set tree_nodes(level) = TView.Nodes.Add(tree_nodes(level - 1), _
tvwChild, CreateGUID, text_line)
tree_nodes(level).EnsureVisible
End If
Loop
TView.Nodes.Item(1).EnsureVisible
End Sub
Public Function CreateGUID() As String
Dim i As Long, b(0 To 15) As Byte
If CoCreateGuid(b(0)) = 0 Then
For i = 0 To 15
CreateGUID = CreateGUID & Right$("00" & Hex$(b(i)), 2)
Next i
Else
MsgBox "Error While creating GUID!"
End If
End Function
Hope this is what you want...
Re: Complication with Treeview
Quote:
Originally Posted by
koolsid
Ok this works for me...
But before you do...amend your text file as follows...
TOPROOT
TOPROOT\NULL
TOPROOT\NULL\COMMONDT
TOPROOT\EVENT
TOPROOT\EVENT\ROTO
TOPROOT\EVENT\ROTO\CLSID0001
TOPROOT\EVENT\ROTO\CLSID0001\CLSID_ID
and then try this... i tried it and it works...
vb Code:
Option Explicit
Private Declare Function CoCreateGuid Lib _
"ole32.dll" (pGUID As Any) As Long
Private iFreeFile As Integer
Private Sub Command1_Click()
'~~> Change file name as applicable
LoadNodesFromFileWithFullPath "C:\Temp\1.txt"
End Sub
Private Sub LoadNodesFromFileWithFullPath(Flnm As String)
Dim text_line As String, level As Integer
Dim tree_nodes() As Node, num_nodes As Integer, pos As Long
iFreeFile = FreeFile()
Open Flnm For Input As #iFreeFile
TView.Nodes.Clear
Do While Not EOF(iFreeFile)
Line Input #iFreeFile, text_line
level = UBound(Split(text_line, "\")) + 1
If level > num_nodes Then
num_nodes = level
ReDim Preserve tree_nodes(1 To num_nodes)
End If
pos = InStrRev(text_line, "\")
If pos Then text_line = Mid$(text_line, pos + 1)
If level = 1 Then
Set tree_nodes(level) = TView.Nodes.Add(, , CreateGUID, text_line)
Else
Set tree_nodes(level) = TView.Nodes.Add(tree_nodes(level - 1), _
tvwChild, CreateGUID, text_line)
tree_nodes(level).EnsureVisible
End If
Loop
TView.Nodes.Item(1).EnsureVisible
End Sub
Public Function CreateGUID() As String
Dim i As Long, b(0 To 15) As Byte
If CoCreateGuid(b(0)) = 0 Then
For i = 0 To 15
CreateGUID = CreateGUID & Right$("00" & Hex$(b(i)), 2)
Next i
Else
MsgBox "Error While creating GUID!"
End If
End Function
Hope this is what you want...
hahahaha! Awesome! That is epitome of Unique Keys right there!
Overkill mode Engaged.
[RESOLVED]Re: Complication with Treeview
Ha .. works perfectly... thank you all very much .. Anyways I have a problem reading the text file itself.. as it is another topic I will open another thread.. thanks once again :)
Re: [RESOLVED] Complication with Treeview