Jump to content

find first instance of valid date in listview


Recommended Posts

hello =)

i am trying to find the first instance of a valid date in a listview group (and un-check mark it).  All other items in each group should be check marked.  If there is no valid date in each group then the last item should be un-checked. (basically all checked items are being marked for deletion - i only want to keep an item that has a valid date)

This works but i am not confident in the way i went about it.

#include <Date.au3>
#include <guilistview.au3>
#include <windowsconstants.au3>
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Local $array[21][2]

$array[0][0] = "lemon"
$array[0][1] = "Group 1"
$array[1][0] = "03/2/201111"
$array[1][1] = "Group 1"
$array[2][0] = "candy"
$array[2][1] = "Group 1"

$array[3][0] = "ship"
$array[3][1] = "Group 2"
$array[4][0] = "2013/05/12 03:12:22"
$array[4][1] = "Group 2"
$array[5][0] = "ants"
$array[5][1] = "Group 2"

$array[6][0] = "2011/04/09 11:34:02"
$array[6][1] = "Group 3"
$array[7][0] = "34/23"
$array[7][1] = "Group 3"
$array[8][0] = "pasta"
$array[8][1] = "Group 3"

$array[9][0] = "chilli"
$array[9][1] = "Group 4"
$array[10][0] = "12/12"
$array[10][1] = "Group 4"
$array[11][0] = "1999/03/14 09:19:34"
$array[11][1] = "Group 4"

$array[12][0] = "1989/01/20 01:01:56"
$array[12][1] = "Group 5"
$array[13][0] = "melon"
$array[13][1] = "Group 5"
$array[14][0] = "19/123"
$array[14][1] = "Group 5"

$array[15][0] = "water"
$array[15][1] = "Group 6"
$array[16][0] = "staple"
$array[16][1] = "Group 6"
$array[17][0] = "brocolli"
$array[17][1] = "Group 6"

$array[18][0] = "corn"
$array[18][1] = "Group 7"
$array[19][0] = "1989/12/01 02:21:19"
$array[19][1] = "Group 7"
$array[20][0] = "1989/12"
$array[20][1] = "Group 7"

$iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)
$iExListViewStyle = BitOR($LVS_REPORT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_DOUBLEBUFFER, $LVS_EX_CHECKBOXES)

GUICreate("test", 400, 400)
$listview = GUICtrlCreateListView("Item", 2, 2, 398, 398, -1, $iExWindowStyle)

_GUICtrlListView_SetColumnWidth($listview, 0, 150)
_GUICtrlListView_SetExtendedListViewStyle($listview, $iExListViewStyle)

_GUICtrlListView_EnableGroupView($listview)

;populate
For $x = 0 To UBound($array) - 1
    $item = $array[$x][0]
    $group = $array[$x][1]

    $idx = _GUICtrlListView_AddItem($listview, $item)
    _GUICtrlListView_InsertGroup($listview, -1, StringReplace($group, "Group ", ""), $group)
    _GUICtrlListView_SetItemGroupID($listview, $idx, StringReplace($group, "Group ", ""))
Next

;check for valid dates
$item_count = _GUICtrlListView_GetItemCount($listview)

        $last_group_id = False

        For $x = 0 To $item_count - 1
            $group_id = _GUICtrlListView_GetItemGroupID($listview, $x)

            $date_valid = False

            Do
                $item_name = _GUICtrlListView_GetItemText($listview, $x, 0)

                If $date_valid = False Then
                    $check = _DateIsValid($item_name)

                    If $check = 0 Then
                        _GUICtrlListView_SetItemChecked($listview, $x)
                    Else
                        $date_valid = True
                    EndIf
                Else
                    _GUICtrlListView_SetItemChecked($listview, $x)
                EndIf

                $x += 1

                $last_group_id = _GUICtrlListView_GetItemGroupID($listview, $x)

            Until $group_id <> $last_group_id

            If $date_valid = False Then
                _GUICtrlListView_SetItemChecked($listview, $x - 1, False)
            EndIf

            $x -= 1
        Next


GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUISetState()

While 1
    Sleep(10)
WEnd

Func _Exit()
    Exit
EndFunc

thank you in advance!

Link to comment
Share on other sites

and which of your used dates isn't valid? There are several formats, depending on language they could be valid or not. I see one Date in future, but in  german format is valid, but we 2 never see a sunrise in 201111.

Edited by AutoBert
Link to comment
Share on other sites

I'm no expert, but maybe in this way you dont need to worry if the groups have the same number of items.

#include <Date.au3>
#include <Array.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Local $array[21][2]

$array[0][0] = "lemon"
$array[0][1] = "Group 1"
$array[1][0] = "03/2/201111"
$array[1][1] = "Group 1"
$array[2][0] = "candy"
$array[2][1] = "Group 1"

$array[3][0] = "ship"
$array[3][1] = "Group 2"
$array[4][0] = "2013/05/12 03:12:22"
$array[4][1] = "Group 2"
$array[5][0] = "ants"
$array[5][1] = "Group 2"

$array[6][0] = "2011/04/09 11:34:02"
$array[6][1] = "Group 3"
$array[7][0] = "34/23"
$array[7][1] = "Group 3"
$array[8][0] = "pasta"
$array[8][1] = "Group 3"

$array[9][0] = "chilli"
$array[9][1] = "Group 4"
$array[10][0] = "12/12"
$array[10][1] = "Group 4"
$array[11][0] = "1999/03/14 09:19:34"
$array[11][1] = "Group 4"

$array[12][0] = "1989/01/20 01:01:56"
$array[12][1] = "Group 5"
$array[13][0] = "melon"
$array[13][1] = "Group 5"
$array[14][0] = "1989/01/20 01:01:56"
$array[14][1] = "Group 5"

$array[15][0] = "water"
$array[15][1] = "Group 6"
$array[16][0] = "staple"
$array[16][1] = "Group 6"
$array[17][0] = "brocolli"
$array[17][1] = "Group 6"

$array[18][0] = "corn"
$array[18][1] = "Group 7"
$array[19][0] = "1989/12/01 02:21:19"
$array[19][1] = "Group 7"
$array[20][0] = "1989/12"
$array[20][1] = "Group 7"

$iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)
$iExListViewStyle = BitOR($LVS_REPORT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_DOUBLEBUFFER, $LVS_EX_CHECKBOXES)

GUICreate("EXAMPLE")
$idListview = GUICtrlCreateListView("Item", 2, 2, 398, 398, -1, $iExWindowStyle)

_GUICtrlListView_SetColumnWidth($idListview, 0, 250)
_GUICtrlListView_SetExtendedListViewStyle($idListview, $iExListViewStyle)

; Add items & Build groups
_GUICtrlListView_EnableGroupView($idListview)
Local $iCurrentGroup, $iLastCreatedGroup
For $i = 0 To UBound($array)-1
    _GUICtrlListView_AddItem($idListview, $array[$i][0])

    $iCurrentGroup = Number(StringReplace($array[$i][1], "Group ", ""))
    If $iCurrentGroup <> $iLastCreatedGroup Then
        _GUICtrlListView_InsertGroup($idListview, -1, $iCurrentGroup, $array[$i][1])
        $iLastCreatedGroup = $iCurrentGroup
    EndIf

    _GUICtrlListView_SetItemGroupID($idListview, $i, $iCurrentGroup)
Next

Local $iGroupCount = _GUICtrlListView_GetGroupCount($idListview)
Local $bFirstDateIsValidFound, $aGroupIndex, $sItemText
For $i = 1 To $iGroupCount
    $bFirstDateIsValidFound = False
    $aGroupIndex = _ArrayFindAll($array, "Group " & $i, Default, Default, Default, Default, 2)
    If Not @error And IsArray($aGroupIndex) Then
        For $j = 0 To UBound($aGroupIndex) - 1
            $sItemText = $array[$aGroupIndex[$j]][0]

            If (_DateIsValid($sItemText) And Not $bFirstDateIsValidFound) Or (($j = (UBound($aGroupIndex) - 1)) And Not $bFirstDateIsValidFound) Then
                $bFirstDateIsValidFound = True
                ContinueLoop
            EndIf

            _GUICtrlListView_SetItemChecked($idListview, $aGroupIndex[$j])
        Next
    EndIf
Next

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit1")

GUISetState()

While 1
    Sleep(100)
WEnd

Func _Exit1()
    Exit
EndFunc

Exit

 

Link to comment
Share on other sites

You can also change the way that you populate the groups. Making it in a way you dont need to worry about the item order in the array, then no matter order, you will always bel able to set the right group order with different numbers of elements.

Edited by MichaelHB
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...