Jump to content

ListView save/load checked scenarios - need an idea


Recommended Posts

Hello all,

I have an ListView with checkboxes and about 300 - 400 items. Entire list is loaded (from ini) every time and I need to store and load "checked scenario" by name. 

Store list of checked indexless is a bad idea because in the case of sorting the list - scenario will no longer be correct.

What is the best way to store list of various scenarios of checked items?

Base code and database ini are attached 

Example_LV_Store_Scenario.au3 Example.ini

Link to comment
Share on other sites

6 hours ago, Luke94 said:

I'd look into using an SQLite database

Never been used this UDF. It not seems like overkill to use it? 

In any case, I have no idea how to do it at the moment. 

Another idea / example?

 

Link to comment
Share on other sites

  • 2 weeks later...

Here is the solution, based on presumption that third ListView column (Name) is unique:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         myName

 Script Function:
    Template AutoIt script.
List
#ce ----------------------------------------------------------------------------

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <ListBoxConstants.au3>

$t = TimerInit()
$aList = IniReadSection("Example.ini", "List")
ConsoleWrite("- Load INI Time " & TimerDiff($t) & @CRLF)


$hWnd = GUICreate("Save Setup Example", 726, 434, 188, 227)
$hLV = GUICtrlCreateListView("", 8, 8, 513, 377)
_GUICtrlListView_SetExtendedListViewStyle($hLV, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
GUICtrlCreateLabel("Search", 16, 400, 38, 17)
$hSearch = GUICtrlCreateInput("Search", 64, 400, 225, 21)

GUICtrlCreateLabel("Scenario", 528, 8, 50, 17)
$hSetup = GUICtrlCreateList("", 528, 32, 187, 110)
GUICtrlSendMsg($hSetup, $LB_INSERTSTRING, 0, "Select All") ; Set Default data
GUICtrlSendMsg($hSetup, $LB_INSERTSTRING, 1, "Clear All") ; Set Default data
    GUICtrlSendMsg($hSetup, $LB_INSERTSTRING, 2, "My Scenario")
    GUICtrlSendMsg($hSetup, $LB_INSERTSTRING, 3, "Scenario Test 2")
    GUICtrlSendMsg($hSetup, $LB_INSERTSTRING, 4, "Test 3")
GUICtrlSendMsg($hSetup, $LB_SELECTSTRING, -1, "Clear All") ; Select in Config
$hSetupSave = GUICtrlCreateButton("Save Scenario", 528, 152, 91, 25)
$hSetupDel = GUICtrlCreateButton("Delete Scenario", 624, 152, 91, 25)


$Debug1 = GUICtrlCreateButton("Select Rng", 528, 312, 91, 25)
$Debug2 = GUICtrlCreateButton("Debug", 624, 312, 91, 25)
$Label3 = GUICtrlCreateLabel("Label3", 528, 344, 36, 17)

; Add columns
_GUICtrlListView_AddColumn($hLV, "#", 45)
_GUICtrlListView_AddColumn($hLV, "Group", 100)
_GUICtrlListView_AddColumn($hLV, "Test Name", 300)
_GUICtrlListView_AddColumn($hLV, "Status", 60)

$t = TimerInit()
_GUICtrlListView_BeginUpdate($hLV)
$NumLen = StringLen($aList[0][0])
For $i = 1 To $aList[0][0]
    _GUICtrlListView_AddItem($hLV, StringFormat("%0" & $NumLen & "i", $i))

    $aStr = StringSplit($aList[$i][0], "__", 1)
    _GUICtrlListView_AddSubItem($hLV, $i-1, StringReplace($aStr[1], "_", " "), 1)
    _GUICtrlListView_AddSubItem($hLV, $i-1, StringReplace($aStr[2], "_", " "), 2) ; GUICtrlCreateListViewItem("Item " & $i, $idListview)
Next
_GUICtrlListView_EndUpdate($hLV)
ConsoleWrite("- Load LV Time " & TimerDiff($t) & @CRLF)


GUISetState(@SW_SHOW)
$iI = -1
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $hSetup
            $setup_value = GUICtrlRead($hSetup)
            Switch $setup_value
                Case "Select All"
                    CheckAll(True)
                Case "Clear All"
                    CheckAll(False)
                Case Else
                    CheckScenario($setup_value)
            EndSwitch

        Case $hSetupSave
            $setup_value = GUICtrlRead($hSetup)
            Switch $setup_value
                Case "Select All","Clear All"
                Case Else
                    SaveScenario($setup_value)
            EndSwitch

        Case $hSetupDel
            $setup_value = GUICtrlRead($hSetup)
            Switch $setup_value
                Case "Select All","Clear All"
                Case Else
                    DelScenario($setup_value)
            EndSwitch

        Case $Debug1
            $iI = Random(0, 10, 1)
            _GUICtrlListView_SetItemSelected($hLV, $iI)
            _GUICtrlListView_SetItemFocused($hLV, $iI)
            _GUICtrlListView_EnsureVisible($hLV, $iI + 1)

    EndSwitch
WEnd

Func CheckAll($on_off)
    For $i = 0 To _GUICtrlListView_GetItemCount($hLV)
        _GUICtrlListView_SetItemChecked($hLV, $i, $on_off)
    Next
EndFunc

Func CheckScenario($scenario_name)
    $aListCheckScenario = IniReadSection("Example.ini", $scenario_name)
    If @error Then
        CheckAll(False)
        Return
    EndIf

    ; comma separated list of checked names from INI for this scenario, if name contains comma then use pipe | as separator
    $names = ','
    For $i = 1 To $aListCheckScenario[0][0]
        $names &= $aListCheckScenario[$i][0] & ','
    Next

    For $i = 0 To _GUICtrlListView_GetItemCount($hLV)
        $name = _GUICtrlListView_GetItemText($hLV, $i, 2)
        If StringInStr($names,','&$name&',') Then
            _GUICtrlListView_SetItemChecked($hLV, $i, True)
        Else
            _GUICtrlListView_SetItemChecked($hLV, $i, False)
        EndIf
    Next
EndFunc

Func SaveScenario($scenario_name)
    $ini_section = ''
    For $i = 0 To _GUICtrlListView_GetItemCount($hLV)
        If _GUICtrlListView_GetItemChecked($hLV, $i) Then
            $name = _GUICtrlListView_GetItemText($hLV, $i, 2)
            $ini_section &= $name & '=1' & @LF
        EndIf
    Next
    IniWriteSection("Example.ini", $scenario_name, $ini_section)
EndFunc

Func DelScenario($scenario_name)
    $i = GUICtrlSendMsg($hSetup, $LB_FINDSTRING, -1, $scenario_name)
    If $i <> -1 Then GUICtrlSendMsg($hSetup, $LB_DELETESTRING, $i, '')
    IniWriteSection("Example.ini", $scenario_name, '')
EndFunc

 

INI sections with checked names:

Quote

[My Scenario]
aLMdyx jtwtEkFOpzlIVdhsg KWNhIuchpGCrYVH=1
FFeFkGdHAG pczzMSfGWozCKcZHZkmilyUHJboMO=1
[Scenario Test 2]
VofOBJcANq ccc TtaufteWWjNpJwZgEsnWYUbZX=1
IGGXZufFwGlDXqRrcegcRDvtmJFNaCGuPvATVFNT=1
rxMZrPPxRVvKoTYpmSJSWBXrjvkCbdecVevVbqUo=1
 

lv_check_scenario.png

Edited by Zedna
Link to comment
Share on other sites

Here is little improvement/optimization:

On SelectionChange in Listbox with scenarios now dynamically enable/disable SaveScenario/DelScenario buttons (disable for Select All/Clear All) and those easier calling SaveScenario()/DelScenario() functions. Due to this better structurization of code is main program loop shorter and more readable:

; https://www.autoitscript.com/forum/topic/208545-listview-saveload-checked-scenarios-need-an-idea/?do=findComment&comment=1505446

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         myName

 Script Function:
    Template AutoIt script.
List
#ce ----------------------------------------------------------------------------

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <ListBoxConstants.au3>

$t = TimerInit()
$aList = IniReadSection("Example.ini", "List")
ConsoleWrite("- Load INI Time " & TimerDiff($t) & @CRLF)


$hWnd = GUICreate("Save Setup Example", 726, 434, 188, 227)
$hLV = GUICtrlCreateListView("", 8, 8, 513, 377)
_GUICtrlListView_SetExtendedListViewStyle($hLV, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
GUICtrlCreateLabel("Search", 16, 400, 38, 17)
$hSearch = GUICtrlCreateInput("Search", 64, 400, 225, 21)

GUICtrlCreateLabel("Scenario", 528, 8, 50, 17)
$hSetup = GUICtrlCreateList("", 528, 32, 187, 110)
GUICtrlSendMsg($hSetup, $LB_INSERTSTRING, 0, "Select All") ; Set Default data
GUICtrlSendMsg($hSetup, $LB_INSERTSTRING, 1, "Clear All") ; Set Default data
    GUICtrlSendMsg($hSetup, $LB_INSERTSTRING, 2, "My Scenario")
    GUICtrlSendMsg($hSetup, $LB_INSERTSTRING, 3, "Scenario Test 2")
    GUICtrlSendMsg($hSetup, $LB_INSERTSTRING, 4, "Test 3")
GUICtrlSendMsg($hSetup, $LB_SELECTSTRING, -1, "Clear All") ; Select in Config
$hSetupSave = GUICtrlCreateButton("Save Scenario", 528, 152, 91, 25)
$hSetupDel = GUICtrlCreateButton("Delete Scenario", 624, 152, 91, 25)
EnableButtonsSaveDel($GUI_DISABLE)

$Debug1 = GUICtrlCreateButton("Select Rng", 528, 312, 91, 25)
$Debug2 = GUICtrlCreateButton("Debug", 624, 312, 91, 25)
$Label3 = GUICtrlCreateLabel("Label3", 528, 344, 36, 17)

; Add columns
_GUICtrlListView_AddColumn($hLV, "#", 45)
_GUICtrlListView_AddColumn($hLV, "Group", 100)
_GUICtrlListView_AddColumn($hLV, "Test Name", 300)
_GUICtrlListView_AddColumn($hLV, "Status", 60)

$t = TimerInit()
_GUICtrlListView_BeginUpdate($hLV)
$NumLen = StringLen($aList[0][0])
For $i = 1 To $aList[0][0]
    _GUICtrlListView_AddItem($hLV, StringFormat("%0" & $NumLen & "i", $i))

    $aStr = StringSplit($aList[$i][0], "__", 1)
    _GUICtrlListView_AddSubItem($hLV, $i-1, StringReplace($aStr[1], "_", " "), 1)
    _GUICtrlListView_AddSubItem($hLV, $i-1, StringReplace($aStr[2], "_", " "), 2) ; GUICtrlCreateListViewItem("Item " & $i, $idListview)
Next
_GUICtrlListView_EndUpdate($hLV)
ConsoleWrite("- Load LV Time " & TimerDiff($t) & @CRLF)


GUISetState(@SW_SHOW)
$iI = -1
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $hSetup
            $setup_value = GUICtrlRead($hSetup)
            Switch $setup_value
                Case "Select All"
                    CheckAll(True)
                    EnableButtonsSaveDel($GUI_DISABLE)
                Case "Clear All"
                    CheckAll(False)
                    EnableButtonsSaveDel($GUI_DISABLE)
                Case Else
                    CheckScenario($setup_value)
                    EnableButtonsSaveDel($GUI_ENABLE)
            EndSwitch

        Case $hSetupSave
            SaveScenario(GUICtrlRead($hSetup))

        Case $hSetupDel
            DelScenario(GUICtrlRead($hSetup))

        Case $Debug1
            $iI = Random(0, 10, 1)
            _GUICtrlListView_SetItemSelected($hLV, $iI)
            _GUICtrlListView_SetItemFocused($hLV, $iI)
            _GUICtrlListView_EnsureVisible($hLV, $iI + 1)

    EndSwitch
WEnd

Func EnableButtonsSaveDel($state)
    GUICtrlSetState($hSetupSave, $state)
    GUICtrlSetState($hSetupDel, $state)
EndFunc

Func CheckAll($on_off)
    For $i = 0 To _GUICtrlListView_GetItemCount($hLV)
        _GUICtrlListView_SetItemChecked($hLV, $i, $on_off)
    Next
EndFunc

Func CheckScenario($scenario_name)
    $aListCheckScenario = IniReadSection("Example.ini", $scenario_name)
    If @error Then
        CheckAll(False)
        Return
    EndIf

    ; comma separated list of checked names from INI for this scenario, if name contains comma then use pipe | as separator
    $names = ','
    For $i = 1 To $aListCheckScenario[0][0]
        $names &= $aListCheckScenario[$i][0] & ','
    Next

    For $i = 0 To _GUICtrlListView_GetItemCount($hLV)
        $name = _GUICtrlListView_GetItemText($hLV, $i, 2)
        If StringInStr($names,','&$name&',') Then
            _GUICtrlListView_SetItemChecked($hLV, $i, True)
        Else
            _GUICtrlListView_SetItemChecked($hLV, $i, False)
        EndIf
    Next
EndFunc

Func SaveScenario($scenario_name)
    $ini_section = ''
    For $i = 0 To _GUICtrlListView_GetItemCount($hLV)
        If _GUICtrlListView_GetItemChecked($hLV, $i) Then
            $name = _GUICtrlListView_GetItemText($hLV, $i, 2)
            $ini_section &= $name & '=1' & @LF
        EndIf
    Next
    IniWriteSection("Example.ini", $scenario_name, $ini_section)
EndFunc

Func DelScenario($scenario_name)
    $i = GUICtrlSendMsg($hSetup, $LB_FINDSTRING, -1, $scenario_name)
    If $i <> -1 Then GUICtrlSendMsg($hSetup, $LB_DELETESTRING, $i, '')
    IniWriteSection("Example.ini", $scenario_name, '')
EndFunc

 

EDIT:

little optimization in EnableButtonsSaveDel()

Edited by Zedna
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

×
×
  • Create New...