Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse formatted text into TreeView control

Status
Not open for further replies.

beredon

Programmer
Jan 5, 2001
17
AU
I have a text file with formatted text... It is formatted into a branch-type layout already, but like this:

Code:
"1key" "text" "command"
{
  "1key"
  {
    "1key" "text" "command"
    "2key" "text" "command"
  }
  "2key"
  {
    "1key" "text" "command"
    "2key" "text" "command"
  }
}

I've been trying to parse the text file formatted like this into a TreeView control, but I just can't do it.

Any help is much appreciated.

Thanks in advance.
 
Please, anyone?

I'm still having no luck getting this to happen.
 
:(

I've tried about a dozen different ways to get this to work... I am having absolutely no luck what-so-ever.

I once had it so it could add a new menu and a child until it ran into what should have been a sibling to the previous node. However, it would simply skip that node until it came to a } where it would add a new menu, but no siblings. This is was far as I could get it to work, unfortunately, that piece of code was lost.

Argh.
 
can you show how you would want your treeview to look like?

i think if you could change your input to XML it would be a whole lot easier.
 
Here is what the file looks like:
Code:
"1key" "text" "command"
{
  "1key"
  {
    "1key" "text" "command"
    "2key" "text" "command"
  }
  "2key"
  {
    "1key" "text" "command"
    "2key" "text" "command"
  }
}

I want the treeview to take on the same appearance, so it'd look like this:

Code:
++ "text"
 ++ "text"
  +- "text"
  +- "text"
 ++ "text"
  +- "text"
  +- "text"

There can be up to 40 menus, and up to 100 items per menu.

I cannot convert the layout of the file, because the file is part of an application (my utility will just be a 3rd party program that makes the editing of this file an easier task).
 
maybe this could start you off

[tt]
"mainkey" "mainkeytext" "command"
{
"onekey" "onekeytext" "command"
{
"one1key" "one1keytext" "command"
"one2key" "one2keytext" "command"
}
"twokey" "twokeytext" "command"
{
"two1key" "two1keytext" "command"
"two2key" "two2keytext" "command"
}
}
[/tt]

Code:
  Dim sFile As String
  Dim nFile As Integer
  Dim sLine As String
  Dim nCurrLvl As Long
  Dim nPrevLvl As Long
  Dim sFields() As String
  Dim oParent As Node
  
  sFile = "C:\test.txt"
  nFile = FreeFile
  
  Open sFile For Input As nFile
  Do While Not EOF(nFile)
    Line Input #nFile, sLine
    nCurrLvl = (Len(sLine) - Len(LTrim$(sLine))) / 2
    sLine = LTrim$(sLine)
    sFields = Split(sLine)
    If ((sLine = "{") Or (sLine = "}") Or (sLine = "")) Then
      ' skip
    Else
      If (nCurrLvl = nPrevLvl) Then
        If (oParent Is Nothing) Then
          Set oParent = TreeView1.Nodes.Add(, , sFields(0), sFields(1))
        Else
          Set oParent = TreeView1.Nodes.Add(oParent, , sFields(0), sFields(1))
        End If
      ElseIf (nCurrLvl > nPrevLvl) Then
        Set oParent = TreeView1.Nodes.Add(oParent, tvwChild, sFields(0), sFields(1))
      Else ' (nCurrLvl < nPrevLvl)
        Set oParent = TreeView1.Nodes.Add(oParent.Parent, , sFields(0), sFields(1))
      End If
      nPrevLvl = nCurrLvl
    End If
    oParent.Expanded = True
  Loop
  Close nFile
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top