Jump to content

Create TreeView from INI file


kpu
 Share

Recommended Posts

I think I'm getting closer. Can someon look at this? I get an error on line 48:

For $i = 1 To $var[0][0]

For $i = 1 To $var^ ERROR

#include <GuiConstants.au3>
#include <file.au3>
#include <array.au3>
Dim $e, $i, $var, $file, $TreeView, $TreeView1, $var2, $TreeView2
Dim $TreeView3, $test1, $varx
Dim $var, $file, $x
$file = "logfile1.ini"

If Not IsDeclared('WS_CLIPSIBLINGS') Then Global $WS_CLIPSIBLINGS = 0x04000000
GuiCreate("Form2", 420, 331, 430,346 ,BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

;_IniReadInfo()
_IniGetSectionNames()
GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case Else
   ;;;
    EndSelect
WEnd
Exit

Func _IniGetSectionNames()
$varx = IniReadSectionNames($file)
If @error Then
    MsgBox(4096, "", "Error occured, probably no INI file.")
Else
    For $x = 1 To $varx[0]
        MsgBox(4096, "", $varx[$x])
        _IniReadInfo()
    Next
EndIf
EndFunc

Func _IniReadInfo()
;MsgBox(32,"VarX","Varx = " & $varx[$x])
$var = IniReadSection($file,$varx[$x])
$TreeView1 = GUICtrlCreateTreeView(16, 16, 353, 217)
If @error Then
    MsgBox(4096, "", "Error occured, probably no INI file.")
Else
    $TreeView2 = GUICtrlCreateTreeViewitem("KPU-24",$TreeView1)
    For $i = 1 To $var[0][0]
    GUICtrlCreateTreeViewItem($var[$i][0], $TreeView2)
       MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF );& "Value: " & $var[$i][1])
    Next
EndIf
EndFunc
Link to comment
Share on other sites

I think I'm getting closer. Can someon look at this? I get an error on line 48:

#include <GuiConstants.au3>
#include <file.au3>
#include <array.au3>
Dim $e, $i, $var, $file, $TreeView, $TreeView1, $var2, $TreeView2
Dim $TreeView3, $test1, $varx
Dim $var, $file, $x
$file = "logfile1.ini"

If Not IsDeclared('WS_CLIPSIBLINGS') Then Global $WS_CLIPSIBLINGS = 0x04000000
GuiCreate("Form2", 420, 331, 430,346 ,BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

;_IniReadInfo()
_IniGetSectionNames()
GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case Else
 ;;;
    EndSelect
WEnd
Exit

Func _IniGetSectionNames()
$varx = IniReadSectionNames($file)
If @error Then
    MsgBox(4096, "", "Error occured, probably no INI file.")
Else
    For $x = 1 To $varx[0]
        MsgBox(4096, "", $varx[$x])
        _IniReadInfo()
    Next
EndIf
EndFunc

Func _IniReadInfo()
;MsgBox(32,"VarX","Varx = " & $varx[$x])
$var = IniReadSection($file,$varx[$x])
$TreeView1 = GUICtrlCreateTreeView(16, 16, 353, 217)
If @error Then
    MsgBox(4096, "", "Error occured, probably no INI file.")
Else
    $TreeView2 = GUICtrlCreateTreeViewitem("KPU-24",$TreeView1)
    For $i = 1 To $var[0][0]
    GUICtrlCreateTreeViewItem($var[$i][0], $TreeView2)
       MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF );& "Value: " & $var[$i][1])
    Next
EndIf
EndFunc
Try the code below. I changed around a few things & cleaned it up somewhat (removed variables that weren't being used etc.) so I could understand what you were doing. Hope you don't mind. java script:emoticon(':P', 'smid_8')

smilie

Your main problem was that you were trying to use a local variable "$x" (now $i_SectionIndex) from the _IniGetSectionNames() function in the _IniReadInfo() function. The _IniReadInfo() function doesn't know anything about this variable & creates it on 1st use & assigns a null value to it. So nothing was being added to the treeview because of it.

I also changed the "kpu-24" entry to the name of each section name by replacing it with $varx[$i_SectionIndex]. This seems to be more informative as far as the displaying of the .INI file is concerned.

HTH

Dragonrider

Here's the code.

#include <GuiConstants.au3>
#include <file.au3>
#include <array.au3>
Dim $msg = ""
Dim $file = "logfile1.ini"

Dim $TreeView = _CreateGUI()

_IniGetSectionNames( $file, $TreeView )
GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case Else
  ;;;
    EndSelect
WEnd
Exit

Func _IniGetSectionNames( $file, $TreeView )
    Local $varx = IniReadSectionNames( $file )
    If @error Then
        Local $RetCode = MsgBox(5000, "", "Error occured, probably no INI file.")
        If( $RetCode = 6 ) Then
            Exit()
        EndIf
    Else
        For $i_SectionIndex = 1 To $varx[0]
            MsgBox(4096, "", $varx[$i_SectionIndex])
            _IniReadInfo( $file, $varx, $TreeView, $i_SectionIndex )
        Next
    EndIf
EndFunc

Func _IniReadInfo( $file, $varx, $TreeView, $i_SectionIndex )
   ;MsgBox(32,"VarX","Varx = " & $varx[$i_SectionIndex])

    Local $TreeView2 = GUICtrlCreateTreeViewitem($varx[$i_SectionIndex],$TreeView )
    Local $var = IniReadSection($file,$varx[$i_SectionIndex])
    If @error Then
       ; MsgBox(4096, "", "Error occured, probably no INI file.")
        Return
    Else
        For $i = 1 To $var[0][0]
            GUICtrlCreateTreeViewItem($var[$i][0] & " = " & $var[$i][1], $TreeView2)
            MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF );& "Value: " & $var[$i][1])
        Next
    EndIf
EndFunc

Func _CreateGUI()
    If Not IsDeclared('WS_CLIPSIBLINGS') Then Global $WS_CLIPSIBLINGS = 0x04000000
    GuiCreate("Form2", 420, 331, 430,346 ,BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
    Return GUICtrlCreateTreeView(16, 16, 353, 217)
EndFunc

Treeview_From_INI.au3

Edited by dragonrider
Link to comment
Share on other sites

Try the code below. I changed around a few things & cleaned it up somewhat (removed variables that weren't being used etc.) so I could understand what you were doing. Hope you don't mind. java script:emoticon(':P', 'smid_8')

smilie

Your main problem was that you were trying to use a local variable "$x" (now $i_SectionIndex) from the _IniGetSectionNames() function in the _IniReadInfo() function. The _IniReadInfo() function doesn't know anything about this variable & creates it on 1st use & assigns a null value to it. So nothing was being added to the treeview because of it.

I also changed the "kpu-24" entry to the name of each section name by replacing it with $varx[$i_SectionIndex]. This seems to be more informative as far as the displaying of the .INI file is concerned.

HTH

Dragonrider

Here's the code.

#include <GuiConstants.au3>
#include <file.au3>
#include <array.au3>
Dim $msg = ""
Dim $file = "logfile1.ini"

Dim $TreeView = _CreateGUI()

_IniGetSectionNames( $file, $TreeView )
GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case Else
 ;;;
    EndSelect
WEnd
Exit

Func _IniGetSectionNames( $file, $TreeView )
    Local $varx = IniReadSectionNames( $file )
    If @error Then
        Local $RetCode = MsgBox(5000, "", "Error occured, probably no INI file.")
        If( $RetCode = 6 ) Then
            Exit()
        EndIf
    Else
        For $i_SectionIndex = 1 To $varx[0]
            MsgBox(4096, "", $varx[$i_SectionIndex])
            _IniReadInfo( $file, $varx, $TreeView, $i_SectionIndex )
        Next
    EndIf
EndFunc

Func _IniReadInfo( $file, $varx, $TreeView, $i_SectionIndex )
  ;MsgBox(32,"VarX","Varx = " & $varx[$i_SectionIndex])

    Local $TreeView2 = GUICtrlCreateTreeViewitem($varx[$i_SectionIndex],$TreeView )
    Local $var = IniReadSection($file,$varx[$i_SectionIndex])
    If @error Then
      ; MsgBox(4096, "", "Error occured, probably no INI file.")
        Return
    Else
        For $i = 1 To $var[0][0]
            GUICtrlCreateTreeViewItem($var[$i][0] & " = " & $var[$i][1], $TreeView2)
            MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF );& "Value: " & $var[$i][1])
        Next
    EndIf
EndFunc

Func _CreateGUI()
    If Not IsDeclared('WS_CLIPSIBLINGS') Then Global $WS_CLIPSIBLINGS = 0x04000000
    GuiCreate("Form2", 420, 331, 430,346 ,BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
    Return GUICtrlCreateTreeView(16, 16, 353, 217)
EndFunc
I found 2 bugs in the code I posted.

1. It said that the .INI file may not exist when there were no key/value pairs in a section. and

2. It didn't display the key's value.

Both have been corrected and the original code above has been edited.

Regards

Dragonrider

P.S. It now reads my win.ini just fine when I put it into the same folder as the script & rename it to "Logfile1.ini".

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...