PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Complication with Treeview-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Complication with Treeview

  1. #1

    Thread Starter
    Lively Member putta's Avatar
    Join Date
    Oct 2004
    Location
    Original Citizen Of This Planet
    Posts
    86

    Resolved [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:
    1. Dim Filenum As Integer
    2. Dim Temp As String
    3. Dim Arr() As String
    4. Dim x As Long
    5. Dim k As Integer
    6. Dim nodx As Node
    7.  
    8. Filenum = FreeFile
    9. Open App.Path & "\test.txt" For Input As #Filenum
    10. Do Until EOF(Filenum) = True
    11. Line Input #Filenum, Temp
    12.     If Left$(Temp, 1) = "[" Then
    13.         Arr() = Split(Replace(Replace(Temp, "[", vbNullString), "]", vbNullString), "\")
    14.  
    15.         If UBound(Arr) = 0 Then
    16.             Set nodx = Tv1.Nodes.Add(, , Arr(0), Arr(0))
    17.             nodx.Expanded = True
    18.         Else
    19.             For k = 0 To UBound(Arr)
    20.                 If k > 0 Then
    21.                     Set nodx = Tv1.Nodes.Add(Arr(k - 1), tvwChild, Arr(k), Arr(k))
    22.                 'Else
    23.                  '   Set nodx = Tv1.Nodes.Add(Arr(0), tvwChild, Arr(k), Arr(k))
    24.                 End If
    25.             Next
    26.         End If
    27.         x = x + 1
    28.     End If
    29. Loop
    30. Close #Filenum



    -Putta

  2. #2
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    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.
    Last edited by Xiphias3; Sep 10th, 2009 at 07:38 AM.

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    11,034

    Re: Complication with Treeview

    Perhaps this trick of MartinLiss would be of help.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    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.

  5. #5
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    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:
    1. Option Explicit
    2.  
    3. Private Declare Function CoCreateGuid Lib _
    4. "ole32.dll" (pGUID As Any) As Long
    5.  
    6. Private iFreeFile As Integer
    7.  
    8. Private Sub Command1_Click()
    9.     '~~> Change file name as applicable
    10.     LoadNodesFromFileWithFullPath "C:\Temp\1.txt"
    11. End Sub
    12.  
    13. Private Sub LoadNodesFromFileWithFullPath(Flnm As String)
    14.     Dim text_line As String, level As Integer
    15.     Dim tree_nodes() As Node, num_nodes As Integer, pos As Long
    16.    
    17.     iFreeFile = FreeFile()
    18.     Open Flnm For Input As #iFreeFile
    19.    
    20.     TView.Nodes.Clear
    21.     Do While Not EOF(iFreeFile)
    22.         Line Input #iFreeFile, text_line
    23.         level = UBound(Split(text_line, "\")) + 1
    24.         If level > num_nodes Then
    25.             num_nodes = level
    26.             ReDim Preserve tree_nodes(1 To num_nodes)
    27.         End If
    28.        
    29.         pos = InStrRev(text_line, "\")
    30.         If pos Then text_line = Mid$(text_line, pos + 1)
    31.  
    32.         If level = 1 Then
    33.             Set tree_nodes(level) = TView.Nodes.Add(, , CreateGUID, text_line)
    34.         Else
    35.             Set tree_nodes(level) = TView.Nodes.Add(tree_nodes(level - 1), _
    36.             tvwChild, CreateGUID, text_line)
    37.             tree_nodes(level).EnsureVisible
    38.         End If
    39.     Loop
    40.     TView.Nodes.Item(1).EnsureVisible
    41. End Sub
    42. Public Function CreateGUID() As String
    43.     Dim i As Long, b(0 To 15) As Byte
    44.     If CoCreateGuid(b(0)) = 0 Then
    45.         For i = 0 To 15
    46.             CreateGUID = CreateGUID & Right$("00" & Hex$(b(i)), 2)
    47.         Next i
    48.     Else
    49.         MsgBox "Error While creating GUID!"
    50.     End If
    51. End Function

    Hope this is what you want...
    Attached Images Attached Images  
    Last edited by Siddharth Rout; Sep 10th, 2009 at 08:25 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  6. #6
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: Complication with Treeview

    Quote Originally Posted by koolsid View Post
    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:
    1. Option Explicit
    2.  
    3. Private Declare Function CoCreateGuid Lib _
    4. "ole32.dll" (pGUID As Any) As Long
    5.  
    6. Private iFreeFile As Integer
    7.  
    8. Private Sub Command1_Click()
    9.     '~~> Change file name as applicable
    10.     LoadNodesFromFileWithFullPath "C:\Temp\1.txt"
    11. End Sub
    12.  
    13. Private Sub LoadNodesFromFileWithFullPath(Flnm As String)
    14.     Dim text_line As String, level As Integer
    15.     Dim tree_nodes() As Node, num_nodes As Integer, pos As Long
    16.    
    17.     iFreeFile = FreeFile()
    18.     Open Flnm For Input As #iFreeFile
    19.    
    20.     TView.Nodes.Clear
    21.     Do While Not EOF(iFreeFile)
    22.         Line Input #iFreeFile, text_line
    23.         level = UBound(Split(text_line, "\")) + 1
    24.         If level > num_nodes Then
    25.             num_nodes = level
    26.             ReDim Preserve tree_nodes(1 To num_nodes)
    27.         End If
    28.        
    29.         pos = InStrRev(text_line, "\")
    30.         If pos Then text_line = Mid$(text_line, pos + 1)
    31.  
    32.         If level = 1 Then
    33.             Set tree_nodes(level) = TView.Nodes.Add(, , CreateGUID, text_line)
    34.         Else
    35.             Set tree_nodes(level) = TView.Nodes.Add(tree_nodes(level - 1), _
    36.             tvwChild, CreateGUID, text_line)
    37.             tree_nodes(level).EnsureVisible
    38.         End If
    39.     Loop
    40.     TView.Nodes.Item(1).EnsureVisible
    41. End Sub
    42. Public Function CreateGUID() As String
    43.     Dim i As Long, b(0 To 15) As Byte
    44.     If CoCreateGuid(b(0)) = 0 Then
    45.         For i = 0 To 15
    46.             CreateGUID = CreateGUID & Right$("00" & Hex$(b(i)), 2)
    47.         Next i
    48.     Else
    49.         MsgBox "Error While creating GUID!"
    50.     End If
    51. End Function

    Hope this is what you want...
    hahahaha! Awesome! That is epitome of Unique Keys right there!
    Overkill mode Engaged.

  7. #7

    Thread Starter
    Lively Member putta's Avatar
    Join Date
    Oct 2004
    Location
    Original Citizen Of This Planet
    Posts
    86

    Resolved [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

  8. #8
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: [RESOLVED] Complication with Treeview

    You are welcome
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width