Jump to content

Listview and iniRead


 Share

Recommended Posts

Hello,

I am trying to get the contents of my ini file into the ListView GUI. I can get the content from the input box to the ini file, but I can't get that content back from the ini file into the Listview. It seems like this would be possible, but I haven't figured it.

Here is what I have so far:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

Global $config = IniReadSection("C:\Program Files\Program\config.ini","SECURITY")

$mainForm = GUICreate("Form", 250,250)
$listResSecGrp = GUICtrlCreateListView($config, 100, 50, 125, 150)
_GUICtrlListView_AddItem($listResSecGrp,IniRead("C:\Program Files\Program\config.ini","SECURITY","",""))
GUICtrlCreateLabel("Security Group",25, 25)
$ResSecGrp = GUICtrlCreateInput("", 25, 50, 70, 21)
$btnAdd = GUICtrlCreateButton("Add", 25, 100, 75, 25, $WS_GROUP)
$btnRemove = GUICtrlCreateButton("Remove", 25, 150, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnAdd
            $ResSecGrp1 = GUICtrlRead($ResSecGrp)
            _GUICtrlListView_AddItem($listResSecGrp,$ResSecGrp1)
            IniWrite("C:\Program Files\Program\config.ini","SECURITY",$ResSecGrp1,$ResSecGrp1)
        Case $btnRemove
            $selGroup = _GUICtrlListView_GetItemSelected($listResSecGrp,1)
            IniDelete("C:\Program Files\Program\config.ini","SECURITY",$selGroup)
    EndSwitch
WEnd

Thanks for your help.

Jeff

Link to comment
Share on other sites

  • Moderators

jazzyjeff,

Firstly, try not to mix native and UDF creation commands if you can avoid it - it usually ends in tears. :(

I would suggest that you replace _GUICtrlListView_AddItem with GUICtrlCreateListViewItem. You can replace _GUICtrlListView_GetItemSelected with the (admittedly clumsy) GuiCtrlRead(GUICtrlRead($listResSecGrp)) - read the Help file to understand why you need to use it twice. :D

Then the code you have for loading the ListView is all wrong. You get a 2D array from the ini file with IniReadSection so you need a loop to get all the key/value returns into the ListView. Then when you want to add/delete an item, you add/delete it to the ini with IniWrite/Delete - I would then clear the ListView and reload it from the ini as if you were loading for the first time. That way you always have the ini and the ListView in sync. :D

If you would care to post a dummy ini file I will help you code a function to do what I have just described. :graduated:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Note taken on mixing the native and UDF codes.

I was thinking about using an array to find all the different results, but I am still learning about creating those.

I got the point of:

For $i = 

Next

The I had no idea where to proceed!

If you could provide some help on this, I'd appreciate it. Thanks.

I am not permitted to attached INI files, so I changed the extension to .txt.

config.txt

Thanks.

Link to comment
Share on other sites

  • Moderators

jazztjeff,

I think this should give you the idea: :graduated:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>

Global $sIni_File = @ScriptDir & "\config.ini"

$mainForm = GUICreate("Form", 250, 250)
$listResSecGrp = GUICtrlCreateListView("SECURITY", 100, 50, 125, 150)
; Set column width to size of ListView, not header - you need a few pixels less or you get a scroll bar!
_GUICtrlListView_SetColumnWidth($listResSecGrp, 0, 121)
; Fill the listView
_ListView_Fill()

GUICtrlCreateLabel("Security Group", 25, 25)
$ResSecGrp = GUICtrlCreateInput("", 25, 50, 70, 21)
$btnAdd = GUICtrlCreateButton("Add", 25, 100, 75, 25)
$btnRemove = GUICtrlCreateButton("Remove", 25, 150, 75, 25)

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnAdd
            ; Read the input
            $ResSecGrp1 = GUICtrlRead($ResSecGrp)
            ; If there is something to add
            If $ResSecGrp1 <> "" Then
                ; Add to the ini file
                IniWrite($sIni_File, "SECURITY", $ResSecGrp1, $ResSecGrp1)
                ; Refill the ListView
                _ListView_Fill()
            EndIf
        Case $btnRemove
            ; Read the selected item - it has a "|" at the end so we need to remove that
            $selGroup = StringTrimRight(GUICtrlRead(GUICtrlRead($listResSecGrp)), 1)
            ; As long as something was selected
            If $selGroup <> "" Then
                ; Remove from ini file
                IniDelete($sIni_File, "SECURITY", $selGroup)
                ; Refill the ListView
                _ListView_Fill()
            EndIf
    EndSwitch
WEnd

Func _ListView_Fill()

    ; Clear ListView
    _GUICtrlListView_DeleteAllItems($listResSecGrp)
    ; Read ini file
    Local $config = IniReadSection($sIni_File, "SECURITY")
    ; Loop through the array and fill the ListView
    For $i = 1 To $config[0][0]
        GUICtrlCreateListViewItem($config[$i][0], $listResSecGrp)
    Next

EndFunc   ;==>_ListView_Fill

If the comments are not clear enough, please ask. :(

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks Melba23. Seeing some of your posts you seem to have a pretty good knowledge of AutoIT, so I'm hoping to pick your brain so I know I am understanding this right.

For $i = 1 To $config[0][0]
        GUICtrlCreateListViewItem($config[$i][0], $listResSecGrp)
    Next

An unknown variable ($i) will start looking at another variable ($config), which is essentially looking at the Security section of the ini file. =1 means it is going to start from the first value up until [0][0]. This is where I am a little unsure. So does [0][0] mean that the end value is unknown and it will keep searching the file until there are no more values to find?

GUICTRLCreateListViewItem then adds the values it finds from $config into the list, $listResSecGrp. What is the [$i][0] exactly doing? Is the $i a variable for each line found in the $config file? Have no idea what [0] is doing. The "Next" part then tells the program to search again for another value in the ini file.

Sorry if that doesn't make sense. I'm trying to get a grasp for this, so I don't have bother anyone again and have a better idea of figuring this out for myself. I think I understand most of it.

Link to comment
Share on other sites

  • Moderators

jazzyjeff,

You need to read the Arrays tutorial in the Wiki! :D

But here is a short taster. Think of an array as a list - you identify the elements in the list by their positon in the list. In AutoIt (as in many other programming languages) the elements start at [0]

0  A

1  B

2  C

Here A is in element [0], C is in element [2].

Arrays can have more than 1 dimension - think of a 2D array as a grid - you need to define each dimension:

0   1   2 

0  .   X   .

1  .   .   .

2  .   .   Y

Here the X is element [0][1] and the Y is [2][2].

Now the loop:

For $i = 1 To $config[0][0]
        GUICtrlCreateListViewItem($config[$i][0], $listResSecGrp)
Next

A counting variable $i starts from 1 and will increase by 1 on each pass through the loop (you can change this if required) until it reaches the number held in the [0][0] element of $config. $config is a 2D array created from the key/value pairs of the ini file - IniReadSection puts the count of the pairs into that element and the actual pairs into the other elements of the array as explained in the Help file.

Each time we pass through the loop, we use GUICtrlCreateListViewItem to insert the value held in the [$i][0] element of the $config array into the ListView. Remember that the value of $i increases by 1 on each pass so we work our way through the array.

When we reach the Next, AutoIt adds 1 to the $i variable and checks if it has exceeded the limit we set (the value in $config[0][0]). If it has not then we return to the top of the loop and run through it again. If the value of $i exceeds the limit, the script continues with the code after the loop.

All clear? :graduated:

M23

P.S. There are excellent tutorials here and here which you would probably find useful. :(

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...