Jump to content

Get index of the checkbox that was changed in ListView


user52
 Share

Recommended Posts

I'm looking for something to register which item (by index?) in the listview has been checked, and unchecked (get the index of the checkbox that was changed).

I've tried to do this by getting the listview's current selection (for an index), but noticed that I could check and uncheck the boxes without selecting the corresponding line. The code below works, but my guess is that there is a better way to do this, please share your thoughts. Thanks

I did find this: http://www.autoitscript.com/forum/index.php?showtopic=26323 and found the code quite useful

This is what i came up with:

#include <GUIConstants.au3>
#include <Array.au3>
#include <GUIListView.au3>

Global $check_status
Global $check_status2

GUICreate("My GUI", 800, 600)
$ListView3 = GUICtrlCreateListView("|Status|Name|Company|Email|Phone|Fax|Date", 10, 10, 780, 580, BitOR($LVS_NOSORTHEADER,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS), BitOR($LVS_EX_FULLROWSELECT,$LVS_EX_CHECKBOXES)) ;$LVS_EX_GRIDLINES

;create test listviewitems
    For $i=0 To 300
        GUICtrlCreateListViewItem("||name goeshere|company goeshere|phone goeshere|fax goeshere|date goeshere", $ListView3)
    Next
;check a few of the listviewitems
    For $i=0 To 300 Step 5
        _GUICtrlListViewSetCheckState($ListView3, $i, 1)
    Next

ListViewSetCheckboxes()
ListViewCheckSetStatus()
ListViewAutoWidth()

GUISetState ()
While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case Else
        If $msg = $GUI_EVENT_PRIMARYDOWN Then
            ListViewUpdateCheckStatus()
        EndIf
    EndSelect
WEnd
Exit

Func ListViewUpdateCheckStatus()
    $check_status2 = $check_status
    $check_status = ""
    If _GUICtrlListViewGetItemCount($ListView3) > 0 Then
        For $i = 0 To _GUICtrlListViewGetItemCount($ListView3) - 1 Step 1
            If _GUICtrlListViewGetCheckedState($ListView3,$i) = 1 Then
                If $check_status = "" Then
                    $check_status = $check_status & $i & ":1"
                ElseIf $check_status <> "" Then
                    $check_status = $check_status & "," & $i & ":1"
                EndIf
            ElseIf _GUICtrlListViewGetCheckedState($ListView3,$i) <> 1 Then
                If $check_status = "" Then
                    $check_status = $check_status & $i & ":0"
                ElseIf $check_status <> "" Then
                    $check_status = $check_status & "," & $i & ":0"
                EndIf
            EndIf
        Next
        ListViewCheckStatusChanged()
    EndIf
EndFunc
Func ListViewCheckStatusChanged()
    If $check_status <> $check_status2 Then
        $a = StringSplit($check_status, ",", 1)
        $b = StringSplit($check_status2, ",", 1)
        If IsArray($a) And IsArray($B) Then
            For $i=0 To UBound($B) - 1
                If $a[$i] <> $b[$i] Then
                    If StringInStr($b[$i], ":0") Then _GUICtrlListViewSetItemText($ListView3, $i - 1, 1, "Active*") ;changed
                    If StringInStr($b[$i], ":1") Then _GUICtrlListViewSetItemText($ListView3, $i - 1, 1, "Not Active*") ;changed
                    ListViewAutoWidth()
                EndIf
            Next
        EndIf
        $check_status2=$check_status
    EndIf
EndFunc
Func ListViewSetCheckboxes()
    For $i=0 To _GUICtrlListViewGetItemCount($ListView3) Step 1
        If _GUICtrlListViewGetCheckedState($ListView3,$i) Then
            _GUICtrlListViewSetItemText($ListView3, $i, 1, "Active")
        Else
            _GUICtrlListViewSetItemText($ListView3, $i, 1, "Not Active")
        EndIf
    Next
EndFunc
Func ListViewCheckSetStatus()
    For $i = 0 To _GUICtrlListViewGetItemCount($ListView3) - 1 Step 1
        If _GUICtrlListViewGetCheckedState($ListView3,$i) = 1 Then
            If $check_status = "" Then
                $check_status = $check_status & $i & ":1"
            ElseIf $check_status <> "" Then
                $check_status = $check_status & "," & $i & ":1"
            EndIf
        ElseIf _GUICtrlListViewGetCheckedState($ListView3,$i) <> 1 Then
            If $check_status = "" Then
                $check_status = $check_status & $i & ":0"
            ElseIf $check_status <> "" Then
                $check_status = $check_status & "," & $i & ":0"
            EndIf
        EndIf
    Next
EndFunc
Func ListViewAutoWidth()
    For $i=0 To 4
        GUICtrlSendMsg($ListView3, $LVM_SETCOLUMNWIDTH, $i, -1) ; 0-column index 100-column width (-1 for autowidth)
    Next
EndFunc
Link to comment
Share on other sites

any thoughts?

; Events - ListView
#include <GuiConstants.au3>;Inclusion file for the GUI interface controls
#include <GuiListView.au3>

#region  Global variables
Global $ListView
Global Const $WM_NOTIFY = 0x004E
Global Const $DebugIt = 1

;ListView Events
Global Const $NM_FIRST = 0
Global Const $NM_CLICK = ($NM_FIRST - 2)
Global Const $NM_DBLCLK = ($NM_FIRST - 3)
#endregion End Global variables

Opt("WinTitleMatchMode", 2)

$main_GUI = GUICreate("GuiRegisterMsg Test", 225, 400, 300, 10, BitOR($WS_THICKFRAME, $WS_SIZEBOX))

$ListView = GUICtrlCreateListView("Entry Name|Category", 5, 75, 195, 280, BitOR($LVS_SORTASCENDING, $LVS_SINGLESEL))
_GUICtrlListViewSetColumnWidth ($ListView, 0, 100)
_GUICtrlListViewSetColumnWidth ($ListView, 1, 100)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlCreateListViewItem("Name 1|Category 1", $ListView)
GUICtrlCreateListViewItem("Name 2|Category 2", $ListView)
GUISetState()

;Register WM_NOTIFY  events
GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

While 1
   
   $msg = GUIGetMsg()
   
   Switch $msg
      
    ;-----------------------------------------------------------------------------------------
    ;This case statement exits and updates code if needed
      Case $GUI_EVENT_CLOSE
         Exit
         
         
       ;-----------------------------------------------------------------------------------------
       ;put all the misc. stuff here
        Case Else
            ;;;
   EndSwitch
WEnd

Func ListView_Click()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then    _DebugPrint("$NM_CLICK")
    ;----------------------------------------------------------------------------------------------
EndFunc   ;==>ListView_Click

Func ListView_DoubleClick()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then    _DebugPrint ("$NM_DBLCLK")
    ;----------------------------------------------------------------------------------------------
    MsgBox(0,"Double Clicked", _GUICtrlListViewGetItemText ($ListView, _GUICtrlListViewGetSelectedIndices($ListView)))
EndFunc   ;==>ListView_DoubleClick

;
; WM_NOTIFY event handler
Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event, $hwndFrom, $code
    $tagNMHDR = DllStructCreate("int;int;int", $lParam) ;NMHDR (hwndFrom, idFrom, code)
    If @error Then Return
    $event = DllStructGetData($tagNMHDR, 3)
    Select
    Case $wParam = $ListView
        Select
            Case $event = $NM_CLICK
                ListView_Click ()
            Case $event = $NM_DBLCLK
                ListView_DoubleClick ()
            EndSelect
    EndSelect
    $tagNMHDR = 0
    $event = 0
    $lParam = 0
EndFunc   ;==>WM_Notify_Events

Func _DebugPrint($s_text)
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+===========================================================" & @LF & _
            "-->" & $s_text & @LF & _
            "+===========================================================" & @LF)
EndFunc   ;==>_DebugPrint
Edited by user52
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...