[R] Compter les cases cochées dans une listview

Aide et conseils concernant AutoIt et ses outils.
Règles du forum
.
Répondre
jpascal
Niveau 6
Niveau 6
Messages : 226
Enregistré le : jeu. 16 oct. 2008 16:21
Status : Hors ligne

[R] Compter les cases cochées dans une listview

#1

Message par jpascal »

Bonjour,

Si je créé une Listview et une fonction WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam) avec ce code :
Switch $hWndFrom
      Case $hWndListView
         Switch $iCode
            Case $NM_CLICK
               UpdateStatusBar()

Lorsque je coche une case, la fonction suivante est exécutée :
Func UpdateStatusBar()
   Local $iChecked = 0, $iSelected = 0
   Local $iItemCount = _GUICtrlListView_GetItemCount($g_idListview)
   Local $sText = ''

   ; Lignes sélectionnées
   $iSelected = _GUICtrlListView_GetSelectedCount($g_idListview)
   $sText = $iSelected & " ligne sélectionnée"
   If $iSelected > 1 Then $sText &= "s"
   If @UserName = 'wmoreau' Then _GUICtrlStatusBar_SetText($g_hStatus, $sText, 1)

   ; Cases cochées
   For $i = 0 To $iItemCount - 1
      If _GUICtrlListView_GetItemChecked($g_idListview, $i) Then $iChecked += 1
   Next
   $sText = $iChecked & " case cochée"
   If $iChecked > 1 Then $sText &= "s"
   If @UserName = 'wmoreau' Then _GUICtrlStatusBar_SetText($g_hStatus, $sText)
EndFunc   ;==>UpdateStatusBar

Mais le chiffre retourné n'est pas bon (il correspond au nombre cases cochées AVANT le clic).

J'ai trouvé la solution en modifiant le code :
   Switch $hWndFrom
      Case $hWndListView
         Switch $iCode
            Case $NM_CLICK
               AdlibRegister('UpdateStatusBar', 10)

Et pour ma fonction UpdateStatusBar() j'ajoute à la fin :

Je me demandais juste s'il y avait une autre méthode pour obtenir le même résultat.
Modifié en dernier par jpascal le lun. 25 nov. 2019 11:46, modifié 1 fois.
AutoIt 3.3.16.1 - AutoIt3Wrapper 21.316.1639.1
Avatar du membre
walkson
Modérateur
Modérateur
Messages : 1020
Enregistré le : ven. 12 août 2011 19:49
Localisation : Hurepoix
Status : Hors ligne

Re: [..] Compter les cases cochées dans une listview  

#2

Message par walkson »

Bonjour,
Comme ceci

Code : Tout sélectionner

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <GuiStatusBar.au3>
Global $g_hListView,  $countcheck = 0


    Local $hGUI, $hImage
    $hGUI = GUICreate("(UDF Created) ListView Create", 400, 300)

    Global $g_hListView = _GUICtrlListView_Create($hGUI, "", 2, 2, 394, 268,$LVS_REPORT)
    _GUICtrlListView_SetExtendedListViewStyle($g_hListView, BitOR( $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES,$LVS_EX_CHECKBOXES,$LVS_EX_FULLROWSELECT))
    GUISetState(@SW_SHOW)


    ; Charge des images
    $hImage = _GUIImageList_Create()
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($g_hListView, 0xFF0000, 16, 16))
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($g_hListView, 0x00FF00, 16, 16))
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($g_hListView, 0x0000FF, 16, 16))
    _GUICtrlListView_SetImageList($g_hListView, $hImage, 1)

    ; Ajoute des colonnes
    _GUICtrlListView_InsertColumn($g_hListView, 0, "Column 1", 100)
    _GUICtrlListView_InsertColumn($g_hListView, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn($g_hListView, 2, "Column 3", 100)
	_GUICtrlListView_InsertColumn($g_hListView, 3, "Column 4", 100)
    ; Ajoute des éléments
    _GUICtrlListView_AddItem($g_hListView, "Row 1: Col 1", 0)
    _GUICtrlListView_AddSubItem($g_hListView, 0, "Row 1: Col 2", 1)
    _GUICtrlListView_AddSubItem($g_hListView, 0, "Row 1: Col 3", 2)
    _GUICtrlListView_AddItem($g_hListView, "Row 2: Col 1", 1)
    _GUICtrlListView_AddSubItem($g_hListView, 1, "Row 2: Col 2", 1)
    _GUICtrlListView_AddItem($g_hListView, "Row 3: Col 1", 2)
	Global $g_hStatus = _GUICtrlStatusBar_Create($hGUI)
	$countcheck = _GUICtrlListView_GetItemCount($g_hListView)
	GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    ; Boucle jusqu'à ce que l'utilisateur quitte.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()


Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    ; Local $tBuffer
    $hWndListView = $g_hListView
    If Not IsHWnd($g_hListView) Then $hWndListView = GUICtrlGetHandle($g_hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom

        Case $hWndListView

            Switch $iCode
               Case $LVN_ITEMCHANGED
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    Local $iItem = DllStructGetData($tInfo, "Item")
					Local $Changed = DllStructGetData($tInfo, "NewState")
					Local $Changed1 = DllStructGetData($tInfo, "OldState")
					Local $yCount = 0
					For $i = 0 To $countcheck - 1
						If  _GUICtrlListView_GetItemChecked ( $hWndFrom, $i) Then
							$yCount += 1
						EndIf

					Next
					_DebugPrint("OldState " & $Changed1 &">NewState "& $Changed &">item "& $iItem & " coché "& _GUICtrlListView_GetItemChecked($g_hListView, $iItem)&">>nombre " & $yCount & @CR)
					UpdateStatusBar($yCount)
					Return False
                Case $NM_CLICK
                    ; Envoyé par un contrôle ListView quand l'utillisateur clique sur un élément avec le bouton gauche de la souris
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
					$rep = Not _GUICtrlListView_GetItemChecked ( $hWndFrom, DllStructGetData($tInfo, "Index") )
                    _DebugPrint("$NM_CLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF )
							_GUICtrlStatusBar_SetText($g_hStatus, _GUICtrlStatusBar_GetText ( $g_hStatus, 0 ) & @TAB & @TAB & _GUICtrlListView_GetItemText( $hWndFrom, DllStructGetData($tInfo, "Index") ))



            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber)
    ConsoleWrite( _
            "+======================================================" & @CRLF & _
            "++>Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint
Func UpdateStatusBar($iChecked)

        Local  $iSelected = 0
        Local $iItemCount = $countcheck
        Local $sText = ''

        ; Lignes sélectionnées
        $iSelected = _GUICtrlListView_GetSelectedCount($g_hListView)
        $sText = $iSelected & " ligne sélectionnée"
        If $iSelected > 1 Then $sText &= "s"
        If @UserName  Then _GUICtrlStatusBar_SetText($g_hStatus, $sText)
        $sText1 = $iChecked & " case cochée"
        If $iChecked > 1 Then $sText &= "s"
        If @UserName  Then _GUICtrlStatusBar_SetText($g_hStatus, $sText1 & " " & $sText)
EndFunc   ;==>UpdateStatusBar

à priori, je dirais que c'est moins énergivore mais ça reste à prouver...
Cordialement,
Walkson
"Horas non numero nisi serenas " Le canon de midi
(Je ne compte que les heures heureuses)
jpascal
Niveau 6
Niveau 6
Messages : 226
Enregistré le : jeu. 16 oct. 2008 16:21
Status : Hors ligne

Re: [..] Compter les cases cochées dans une listview

#3

Message par jpascal »

Utiliser l'événement $LVN_ITEMCHANGED, tout simplement.

Une connaissance de plus grâce à vous, merci. :-)
AutoIt 3.3.16.1 - AutoIt3Wrapper 21.316.1639.1
Répondre