Jump to content

Reading from an INI file and displaying as a list


Recommended Posts

Hi ! I am learning autoit and was trying to make a code that will read info from an INI file section and display its contents in a list. I wrote this rough code. The INI file is something like this-

[section1]

data1 = value1

data2 = value2

etc.

Though the list of data is displayed correctly, the values arent. Please guide me. I would also like to keep some feature such that I can select a data-value pair from the list, and delete it, such that it is removed from the ini file as well. Please suggest a solution.

#include <GUIConstantsEx.au3>
#include <DateTimeConstants.au3>
#include <Process.au3>
#include <GUIConstants.au3>
#include <GUIListBox.au3>
#include <GuiListView.au3>
#Include <Date.au3>
#include <constants.au3>

AutoItSetOption ("ExpandVarStrings" , 1)
Local $exStyles = BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES)

$guiapp = GUICreate("test" , 400, 500,-1, -1, -1, 0x00000018); WS_EX_ACCEPTFILES
$listview = GUICtrlCreateListView("", 10, 32, 300, 197)
_GUICtrlListView_SetExtendedListViewStyle($listview, $exStyles)

GUICtrlSetLimit(-1, 200)    ; to limit horizontal scrolling
GUICtrlSetData(-1, "")

    $msg = GUICtrlCreateButton("Done", 160, 450, 90)
GUISetState()

$var = IniReadSection(@ScriptDir&"\data.ini", "section1")
_GUICtrlListView_AddColumn($listview, "Column 1", 120)
_GUICtrlListView_AddColumn($listview, "Column 2", 120)

For $i = 1 To $var[0][0]
    _GUICtrlListView_AddItem($listview, $var[$i][0], 0)
    
Next

$i = 1

For $i = 1 To $var[0][0]
_GUICtrlListView_AddSubItem($listview, 1, $var[$i][1], 1, $i)
Next

    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
    WEnd
Link to comment
Share on other sites

Hi,

this should diplay your listview as you want:

#include <GUIConstantsEx.au3>
#include <DateTimeConstants.au3>
#include <Process.au3>
#include <GUIConstants.au3>
#include <GUIListBox.au3>
#include <GuiListView.au3>
#Include <Date.au3>
#include <constants.au3>


AutoItSetOption ("ExpandVarStrings" , 1)
Local $exStyles = BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES)

$guiapp = GUICreate("test" , 400, 500,-1, -1, -1, 0x00000018); WS_EX_ACCEPTFILES
$listview = GUICtrlCreateListView("", 10, 32, 300, 197)
_GUICtrlListView_SetExtendedListViewStyle($listview, $exStyles)

GUICtrlSetLimit(-1, 200)    ; to limit horizontal scrolling
GUICtrlSetData(-1, "")

$msg = GUICtrlCreateButton("Done", 160, 450, 90)
GUISetState()

$var = IniReadSection(@ScriptDir&"\data.ini", "section1")
;_ArrayDisplay ($var)
_GUICtrlListView_AddColumn($listview, "Column 1", 120)
_GUICtrlListView_AddColumn($listview, "Column 2", 120)

For $i = 1 To $var[0][0]
    _GUICtrlListView_AddItem($listview, $var[$i][0], 0)
    _GUICtrlListView_AddSubItem($listview, $i - 1, $var[$i][1], 1, $i)
Next

While $msg <> $GUI_EVENT_CLOSE
    $msg = GUIGetMsg()
WEnd

;-))

Stefan

Link to comment
Share on other sites

Thanks Stefan ! This worked like a charm ... Can you plz guide me with the other part, viz, how can I select a entry from the list and delete it from the list and the INI file as well.

And if I can edit that entry in INI file by selecting it from the list, that will be a bliss ;-)

Thanks in advance.

Edited by dexter
Link to comment
Share on other sites

Hi,

some modifications as well. The deletion is based on clicking Button <Done>. You may modify the code.

#include <GUIConstantsEx.au3>
#include <DateTimeConstants.au3>
#include <Process.au3>
#include <GUIConstants.au3>
#include <GUIListBox.au3>
#include <GuiListView.au3>
#Include <Date.au3>
#include <constants.au3>
#include <array.au3>

AutoItSetOption ("ExpandVarStrings" , 1)
Local $exStyles = BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES)
Local $msg
$guiapp = GUICreate("test" , 400, 500,-1, -1, -1, 0x00000018); WS_EX_ACCEPTFILES
;changing creation to _Gui... function
$listview = _GUICtrlListView_Create ($guiapp,"", 10, 32, 300, 197)
_GUICtrlListView_SetExtendedListViewStyle($listview, $exStyles)

GUICtrlSetLimit(-1, 200)    ; to limit horizontal scrolling
GUICtrlSetData(-1, "")
;changing variable name from $msg to $done
$done = GUICtrlCreateButton("Done", 160, 450, 90)
GUISetState()

$var = IniReadSection(@ScriptDir&"\data.ini", "section1")
;delete 1 st element -> element count of array to use 
_ArrayDelete ($var, 0)
_GUICtrlListView_AddColumn($listview, "Column 1", 120)
_GUICtrlListView_AddColumn($listview, "Column 2", 120)
;creating listview based on array -> see above
_GUICtrlListView_AddArray ($listview, $var)
_GUICtrlListView_SetItemSelected($listview, 0)

While $msg <> $GUI_EVENT_CLOSE
    $msg = GUIGetMsg()
    ;button Done is clicked
    If $msg = $done Then
        ;this based on !! one !! selected item. if you want multi selection have a look at helpfile
        ;_GUICtrlListView_GetSelectedIndices -> return array
        ;then loop over array and do appropriate action
        $index = _GUICtrlListView_GetSelectedIndices ($listview)
        If $index <> "" Then
            ;delete listview items
            _GUICtrlListView_DeleteItem($listview, $index)
            ;delete item in ini file
            IniDelete (@ScriptDir&"\data.ini", "section1", $var [$index] [0])
            ;delete item in array
            _ArrayDelete ($var, $index)
            ;check only by opening ini file
            ShellExecute ("notepad", "data.ini")
        EndIf
    EndIf
    
WEnd

;-))

Stefan

Edited by 99ojo
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...