Sign in to follow this  
Followers 0
xeroTechnologiesLLC

[Resolved] read ini to listbox, rearrange in list, rewrite to ini on save

11 posts in this topic

#1 ·  Posted (edited)

I'm trying to read data from an .ini file into a list box (in the order it is in in the list box; by default the list box arranges it in a specific order - I need to override this somehow), let the user rearrange the order they are in, then hit a button and rewrite that list, in the new order it's in, back into the ini file.

Thoughts?

What I have so far:

Read data from the ini to the list box - check.

Rearrange in list - working on it with an example using a method found here:

Write what's in the list box back into the ini file in the order that it is in the list box - no clue. I know how to write to an ini section, but not read all the contents out of the list box at once instead of what is just selected...or if there is a way to select all the entries then write it - that would work probably, but again, the order in which the items are in is paramount.

Same thing for a combo box, but if I can get the list to work right I think i can get the combo box relatively easy.

Code so far, for what it's worth...

#include <array.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
$listString = ""
$comboString = ""
$listArray = IniReadSection("test_list.ini", "itemlist")
;~ _ArrayDisplay($listArray)
;~ Exit
$listString = "New|"
For $l = 1 to $listArray[0][0]
if $l <> $listArray[0][0] Then
  $listString &= $listArray[$l][1] & "|"
Else
  $listString &= $listArray[$l][1]
EndIf
Next
;~ MsgBox(0x40000, "", $listString)

$comboArray = IniReadSection("test_combo.ini", "itemlist")
#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("Form2", 360, 305, 428, 264)
$List1 = GUICtrlCreateList("", 16, 16, 161, 253, $LBS_standard)
  ; add list ini array
GUICtrlSetData($List1, $listString)
$Combo1 = GUICtrlCreateCombo("", 192, 16, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
  GUICtrlSetData($Combo1, $listString)
$saveBTN = GUICtrlCreateButton("Save", 192, 100, 100, 30)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $saveBTN
   $listdata = GUICtrlRead($List1) ; will only read what is selected, alternatives?
   IniWriteSection("test_list.ini", "itemlist", $listdata)
EndSwitch
WEnd

test_list.ini file(same directory as the test code noted above):

[itemlist]
1=1
2=2
3=3
4=4
5=5
6=6
7=7
8=8
9=9
10=10
Edited by xeroTechnologiesLLC

Share this post


Link to post
Share on other sites



Use _GUICtrlListBox_SelItemRange to select the items, if the $iLast parameter is larger than the total number of items, then it will select from the $iFirst item to the end with no error. Then you can use _GUICtrlListBox_GetSelItemsText to read the selected item's text to an array that you can then write to the INI file.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Awesome.

As for the sorting order when it reads it from the .ini, any thoughts on controlling that part?

Share this post


Link to post
Share on other sites

When you read the items from the listbox, they will be read in the order that they're in the list. When you write them to the ini file, they should be still in that order, it should overwrite any values that are currently in it with the new values. The only problem that I can see is if there are less items in the list than there are in the ini file, because if you have 10 items in the ini, and only 8 in the listbox, the last 2 won't get deleted unless you specifically delete them.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Gotcha.

Reading from the ini to the listbox though, it sorts them a certain way - is there anyway around that?

Share this post


Link to post
Share on other sites

From the help file.

style [optional] Defines the style of the control. See GUI Control Styles Appendix.

default ( -1) : $LBS_SORT, $WS_BORDER, $WS_VSCROLL

forced styles : $WS_TABSTOP, $LBS_NOTIFY

Don't use the $LBS_Standard style when making the listbox, use BitOr($WS_BORDER, $WS_VSCROLL, $WS_TABSTOP, $LBS_NOTIFY) in it's place to keep all of the other style settings.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

AH... RTFM. got it.

LOL

I'll work on this today and when I have something setup I'll post the code and go from there.

Eternal thanks, master BrewManNH!

Share this post


Link to post
Share on other sites

I came across:

...which mostly works for single items but not for a listbox that allows for multiple selections apparently.

Any pointers on dragging and dropping items to rearrange them in a list box?

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

found that, implemented, came up with the below, but it's still not writting to the .ini file. I'll keep working on it.

#include <array.au3>
#include <Constants.au3>
#include <ComboConstants.au3>
#include <DragList_UDF.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
Global $listString = ""
Global $comboString = ""
$Debug_LB = False ; Check ClassName being passed to ListBox functions, set to True and use a handle to another control to see it work
$listArray = IniReadSection("test_list.ini", "itemlist")
$comboArray = IniReadSection("test_combo.ini", "itemlist")
#Region ### START Koda GUI section ### Form=
$hMain = GUICreate("Form2", 360, 305, 428, 264)
$hListBox = GUICtrlCreateList("", 16, 16, 161, 253, BitOR($WS_BORDER, $WS_VSCROLL, $WS_TABSTOP, $LBS_NOTIFY, $LBS_EXTENDEDSEL))
$Combo1 = GUICtrlCreateCombo("", 192, 16, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
  GUICtrlSetData($Combo1, $listString)
$saveBTN = GUICtrlCreateButton("Save", 192, 100, 100, 30)
$rearrangeChk = GUICtrlCreateCheckbox("Rearrange On", 192, 75)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $wProcNew = DllCallbackRegister("_MyWndProc", "int", "hwnd;int;wparam;lparam")
Global $wProcOld = _WinAPI_SetWindowLong($hMain, $GWL_WNDPROC, DllCallbackGetPtr($wProcNew))
    ; Add strings
    _GUICtrlListBox_BeginUpdate($hListBox)
_GUICtrlListBox_AddString($hListBox, "New")
    For $iI = 1 To $listArray[0][0]
        _GUICtrlListBox_AddString($hListBox, $listArray[$iI][1])
    Next
    _GUICtrlListBox_EndUpdate($hListBox)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   _WinAPI_DeleteObject($wProcOld)
   Exit
  Case $saveBTN
   ; Select a few items
   _GUICtrlListBox_SelItemRange($hListBox, 1, $listArray[0][0])
   $listRead = GUICtrlRead($hListBox)
   MsgBox(0x40000, "", $listRead)
   IniWriteSection("test_combo.ini", "itemlist", $listRead)
  case $rearrangeChk
   If GUICtrlRead($rearrangeChk) = $GUI_CHECKED Then
    _DragList_SetList($hListBox, $hMain)
   Else
    _DragList_SetList($hListBox)
   EndIf
EndSwitch
WEnd

edit:

_GUICtrlListBox_SelItemRange($hListBox, 1, $listArray[0][0])
            $listRead = GUICtrlRead($hListBox)
            MsgBox(0x40000, "", $listRead)
            IniWriteSection("test_list.ini", "itemlist", $listRead)

...wrong ini file.

however it seems to only be writing 1 or 2 items, not the entire selection.

further edit:

updated the save button to:

Case $saveBTN
   ; Select a few items
   $x = _GUICtrlListBox_SelItemRange($hListBox, 1, $listArray[0][0])
   if $x = True Then
    $listRearrange = _GUICtrlListBox_GetSelItems($hListBox)
    _ArrayDisplay($listRearrange)
     For $xL = 1 to $listRearrange[0]
      IniWrite("test_list.ini", "itemlist", $xL, $listRearrange[$xL])
     Next
   Else
    Sleep(100)
   EndIf

...but the problem now is that it's sorting all the selected items in the array...i need it unsorted still.

Edited by xeroTechnologiesLLC

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Resolved.

Part 1 - just the list box is dynamic in this example. I will edit/update this post when I get the code for the combo box added.

#include <array.au3>
#include <Constants.au3>
#include <ComboConstants.au3>
#include <DragList_UDF.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
Global $listString = ""
Global $comboString = ""
$Debug_LB = False ; Check ClassName being passed to ListBox functions, set to True and use a handle to another control to see it work
$listArray = IniReadSection("test_list.ini", "itemlist")
$comboArray = IniReadSection("test_combo.ini", "itemlist")
#Region ### START Koda GUI section ### Form=
    $hMain = GUICreate("Form2", 360, 305, 428, 264)
    $hListBox = GUICtrlCreateList("", 16, 16, 161, 253, BitOR($WS_BORDER, $WS_VSCROLL, $WS_TABSTOP, $LBS_NOTIFY, $LBS_EXTENDEDSEL))
    $Combo1 = GUICtrlCreateCombo("", 192, 16, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
        GUICtrlSetData($Combo1, $listString)
    $saveBTN = GUICtrlCreateButton("Save", 192, 100, 100, 30)
    $rearrangeChk = GUICtrlCreateCheckbox("Rearrange On", 192, 75)
    GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


    ; Add strings
    _GUICtrlListBox_BeginUpdate($hListBox)
    _GUICtrlListBox_AddString($hListBox, "New")
    For $iI = 1 To $listArray[0][0]
        _GUICtrlListBox_AddString($hListBox, $listArray[$iI][1])
    Next
    _GUICtrlListBox_EndUpdate($hListBox)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $saveBTN
            ; Select a few items
            $x = _GUICtrlListBox_SelItemRange($hListBox, 1, $listArray[0][0])
            if $x = True Then
                $listRearrange = _GUICtrlListBox_GetSelItemsText($hListBox)
                _ArrayDisplay($listRearrange)
                    For $xL = 1 to $listRearrange[0]
                        IniWrite("test_list.ini", "itemlist", $xL, $listRearrange[$xL])
                    Next
            Else
                Sleep(100)
            EndIf
        case $rearrangeChk
            If GUICtrlRead($rearrangeChk) = $GUI_CHECKED Then
                _DragList_SetList($hListBox, $hMain)
            Else
                _DragList_SetList($hListBox)
            EndIf
    EndSwitch
WEnd

The only downside is that I can't drag items below the bottom item. Ex: if i drag 1 down to 10, it'll take the place of 9, not 10.

Any ideas?

And the accompanying .ini structure:

[itemlist]
1=10
2=9
3=8
4=7
5=6
6=5
7=4
8=3
9=2
10=1

edit:

apologies - the purpose of this one was not to make a dynamic drag and drop combo box, but make it read the items from the same ini file that was being altered in the list box.

think of this like a list of items you would run in a specific order and you would select the macro in the combo box, the list has the items, etc....

also removed extra junk code. I will still post the code on this example when it is complete.....

Edited by xeroTechnologiesLLC

Share this post


Link to post
Share on other sites

Please keep in mind this is just a test file, but hopefully someone will find it useful.

#include <array.au3>
#include <Constants.au3>
#include <ComboConstants.au3>
#include <DragList_UDF.au3>
#include <GUIConstantsEx.au3>
#include <GuiComboBoxEx.au3>
#include <GuiImageList.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
Global $listString = ""
Global $comboString = ""
$Debug_LB = False ; Check ClassName being passed to ListBox functions, set to True and use a handle to another control to see it work
$Debug_CB = False ; Check ClassName being passed to ComboBox/ComboBoxEx functions, set to True and use a handle to another control to see it work
$listArray = IniReadSection("test_list.ini", "itemlist")
$comboArray = IniReadSection("test_combo.ini", "itemlist")
#Region ### START Koda GUI section ### Form=
$hMain = GUICreate("Form2", 360, 305, 428, 264)
$hListBox = GUICtrlCreateList("", 16, 16, 161, 253, BitOR($WS_BORDER, $WS_VSCROLL, $WS_TABSTOP, $LBS_NOTIFY, $LBS_EXTENDEDSEL))
$Combo1 = _GUICtrlComboBoxEx_Create($hMain, "", 192, 16, 145, 100)
  $Combo1handle = GUICtrlGetHandle($Combo1)
$saveBTN = GUICtrlCreateButton("Save", 192, 100, 100, 30)
$rearrangeChk = GUICtrlCreateCheckbox("Rearrange On", 192, 75)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

; Add items to combo box
_GUICtrlComboBoxEx_InitStorage($Combo1, $comboArray[0][0] + 1, 300)
_GUICtrlComboBoxEx_BeginUpdate($Combo1)
_GUICtrlComboBoxEx_AddString($Combo1, "New")
For $iC = 1 To $comboArray[0][0]
  _GUICtrlComboBoxEx_AddString($Combo1, $comboArray[$iC][1])
Next
_GUICtrlComboBoxEx_EndUpdate($Combo1)
$placeholder = ""
While 1
$comboText = ControlGetText($hMain, "", "[CLASS:Edit; INSTANCE:1]")
if $comboText <> $placeholder Then
  $placeholder = $comboText
Switch $comboText
  case "Macro 1"
   $macro1Array = ""
   $macro1Array = IniReadSection("testMacro1.ini", "itemlist")
   ; clear the list before loading the macro list
   _GUICtrlListBox_ResetContent($hListBox)
   ; Add strings
   _GUICtrlListBox_BeginUpdate($hListBox)
   _GUICtrlListBox_AddString($hListBox, "New")
   For $iI = 1 To $macro1Array[0][0]
    _GUICtrlListBox_AddString($hListBox, $macro1Array[$iI][1])
   Next
   _GUICtrlListBox_EndUpdate($hListBox)
  case "Macro 2"
   $macro2Array = ""
   $macro2Array = IniReadSection("testMacro2.ini", "itemlist")
   ; clear the list before loading the macro list
   _GUICtrlListBox_ResetContent($hListBox)
   ; Add strings
   _GUICtrlListBox_BeginUpdate($hListBox)
   _GUICtrlListBox_AddString($hListBox, "New")
   For $iI = 1 To $macro2Array[0][0]
    _GUICtrlListBox_AddString($hListBox, $macro2Array[$iI][1])
   Next
   _GUICtrlListBox_EndUpdate($hListBox)

  case "New"
   ; clear the list before loading the macro list
   _GUICtrlListBox_ResetContent($hListBox)
   MsgBox(0x40000, "", "Create a new macro...")
EndSwitch
EndIf

$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $saveBTN
   switch $comboText
    case "New"
     MsgBox(0x40000, "New Macro", "Create a new macro in the .ini files...")
    Case "Macro 1"
     ; Select a few items
     $x = _GUICtrlListBox_SelItemRange($hListBox, 1, $macro1Array[0][0])
     if $x = True Then
      $listRearrange1 = _GUICtrlListBox_GetSelItemsText($hListBox)
       For $xL = 1 to $listRearrange1[0]
        IniWrite("testMacro1.ini", "itemlist", $xL, $listRearrange1[$xL])
       Next
     Else
      Sleep(100)
     EndIf
    case "Macro 2"
     ; Select a few items
     $x = _GUICtrlListBox_SelItemRange($hListBox, 1, $macro2Array[0][0])
     if $x = True Then
      $listRearrange2 = _GUICtrlListBox_GetSelItemsText($hListBox)
       For $xL = 1 to $listRearrange2[0]
        IniWrite("testMacro2.ini", "itemlist", $xL, $listRearrange2[$xL])
       Next
     Else
      Sleep(100)
     EndIf
   EndSwitch
  case $rearrangeChk
   If GUICtrlRead($rearrangeChk) = $GUI_CHECKED Then
    _DragList_SetList($hListBox, $hMain)
   Else
    _DragList_SetList($hListBox)
   EndIf
EndSwitch
WEnd

test_combo.ini

[itemlist]
1=Macro 1
2=Macro 2

testMacro1.ini

[itemlist]
1=10
2=3
3=9
4=2
5=8
6=6
7=1
8=4
9=7
10=5

testMacro2.ini

[itemlist]
1=1
2=2
3=3
4=4
5=5
6=6
7=7
8=8
9=9
10=10

How to use:

Run the code, select a macro in the combo drown down and it should populate that ini contents in the list box on the left.

Click the check box to enable rearranging.

Once you have it arranged the way you want, hit save.

You can then go to the other macro and back to the one you edited and it should have the new arrangement also.

Hope someone finds it helpful.

Thanks to BrewManNH for the assist!

Share this post


Link to post
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
Sign in to follow this  
Followers 0