Jump to content
Sign in to follow this  
EKY32

Detect ListView Items Unchecking

Recommended Posts

EKY32

Hello,

I have a CheckBox to check and uncheck a ListView items, I didn't know the best way to detect items unchecking from the list view to make the CheckBox in the third state ($GUI_INDETERMINATE) which means that not all the items had been selected.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 320, 259, 192, 124)
$Checkbox1 = GUICtrlCreateCheckbox("Select All Items", 8, 24, 97, 17, BitOR($BS_3STATE,$WS_TABSTOP))
$ListView1 = GUICtrlCreateListView("Items", 8, 48, 297, 201)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
_GUICtrlListView_SetExtendedListViewStyle($ListView1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))

$ListView1_0 = _GUICtrlListView_AddItem($ListView1,"one")
$ListView1_1 = _GUICtrlListView_AddItem($ListView1,"two")
$ListView1_2 = _GUICtrlListView_AddItem($ListView1,"three")
$ListView1_3 = _GUICtrlListView_AddItem($ListView1,"four")
$ListView1_4 = _GUICtrlListView_AddItem($ListView1,"five")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Checkbox1
            If GUICtrlRead($Checkbox1) = $GUI_UNCHECKED Then
                GUICtrlSetState($Checkbox1, $GUI_CHECKED)
                For $C = 1 To _GUICtrlListView_GetItemCount($ListView1)
                    _GUICtrlListView_SetItemChecked($ListView1, $C - 1)
                Next
            ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
                GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)
                For $C = 1 To _GUICtrlListView_GetItemCount($ListView1)
                    _GUICtrlListView_SetItemChecked($ListView1, $C - 1, False)
                Next
            EndIf

    EndSwitch
WEnd

Thank you.


[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Share this post


Link to post
Share on other sites
bordomavi

I dont understand what are you need? 

Share this post


Link to post
Share on other sites
Danyfirex

Hi.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 320, 259, 192, 124)
Global $Checkbox1 = GUICtrlCreateCheckbox("Select All Items", 8, 24, 97, 17, BitOR($BS_3STATE,$WS_TABSTOP))
$ListView1 = GUICtrlCreateListView("Items", 8, 48, 297, 201)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
_GUICtrlListView_SetExtendedListViewStyle($ListView1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))

$ListView1_0 = _GUICtrlListView_AddItem($ListView1,"one")
$ListView1_1 = _GUICtrlListView_AddItem($ListView1,"two")
$ListView1_2 = _GUICtrlListView_AddItem($ListView1,"three")
$ListView1_3 = _GUICtrlListView_AddItem($ListView1,"four")
$ListView1_4 = _GUICtrlListView_AddItem($ListView1,"five")

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Checkbox1
            If GUICtrlRead($Checkbox1) = $GUI_UNCHECKED Then
                GUICtrlSetState($Checkbox1, $GUI_CHECKED)
                For $C = 1 To _GUICtrlListView_GetItemCount($ListView1)
                    _GUICtrlListView_SetItemChecked($ListView1, $C - 1)
                Next
            ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
                GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)
                For $C = 1 To _GUICtrlListView_GetItemCount($ListView1)
                    _GUICtrlListView_SetItemChecked($ListView1, $C - 1, False)
                Next
            EndIf

    EndSwitch
WEnd


Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hListView, $tNMHDR, $hWndFrom, $iCode

    $hListView = $ListView1
    If Not IsHWnd($hListView) Then $hListView = GUICtrlGetHandle($ListView1)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "HwndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $LVN_ITEMCHANGED
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    Local $iItem = DllStructGetData($tInfo, "Item")

                    If _GUICtrlListView_GetItemChecked($hListView, $iItem) = False Then
                        GUICtrlSetState($Checkbox1,$GUI_INDETERMINATE)
                        ConsoleWrite("---> Item " & $iItem + 1 & " has Unchecked" & @LF)
                    EndIf
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Saludos

Share this post


Link to post
Share on other sites
mikell

Maybe cleaner using a boolean

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

$Form1 = GUICreate("Form1", 320, 259, 192, 124)
Global $Checkbox1 = GUICtrlCreateCheckbox("Select All Items", 8, 24, 97, 17, BitOR($BS_3STATE,$WS_TABSTOP))
$test = GuiCtrlCreateButton("test all checked", 200, 24, 97, 17)
$ListView1 = GUICtrlCreateListView("Items", 8, 48, 297, 201)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
_GUICtrlListView_SetExtendedListViewStyle($ListView1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))

$ListView1_0 = _GUICtrlListView_AddItem($ListView1,"one")
$ListView1_1 = _GUICtrlListView_AddItem($ListView1,"two")
$ListView1_2 = _GUICtrlListView_AddItem($ListView1,"three")
$ListView1_3 = _GUICtrlListView_AddItem($ListView1,"four")
$ListView1_4 = _GUICtrlListView_AddItem($ListView1,"five")

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)

Global $allchecked = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $test
               Msgbox(0,"", $allchecked)
        Case $Checkbox1
            If GUICtrlRead($Checkbox1) = $GUI_UNCHECKED Then
                GUICtrlSetState($Checkbox1, $GUI_CHECKED)
                For $C = 1 To _GUICtrlListView_GetItemCount($ListView1)
                    _GUICtrlListView_SetItemChecked($ListView1, $C - 1)
                Next
                $allchecked = 1
            ElseIf GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
                GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)
                For $C = 1 To _GUICtrlListView_GetItemCount($ListView1)
                    _GUICtrlListView_SetItemChecked($ListView1, $C - 1, False)
                Next
                $allchecked = 0
            EndIf
    EndSwitch
WEnd


Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hListView, $tNMHDR, $hWndFrom, $iCode

    $hListView = $ListView1
    If Not IsHWnd($hListView) Then $hListView = GUICtrlGetHandle($ListView1)
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "HwndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $LVN_ITEMCHANGED
                    $allchecked = 1
                    For $C = 1 To _GUICtrlListView_GetItemCount($ListView1)
                       If _GUICtrlListView_GetItemChecked($hListView, $C - 1) = False Then
                           GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)
                           $allchecked = 0
                       EndIf
                   Next
                   If $allchecked = 1 Then GUICtrlSetState($Checkbox1, $GUI_CHECKED)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Share this post


Link to post
Share on other sites
EKY32

Thank you all dear friends.


[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

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  

×