Jump to content

check all checkboxes in listview


dickep
 Share

Recommended Posts

I have a listview with checkboxes. I have the first one that I would like to, upon checking it, would check all the others.

I tried some things, but nothing worked. I can't figure out how to find out if it is checked or not in the listview.

Also, that brings up the question of "how do I check that the listview check boxes are checked/unchecked"??

Thanks

E

Link to comment
Share on other sites

Hi, here is an example:

#include <GuiConstants.au3>
#include <GuiListView.au3>
Opt("GuiOnEventMode", 1)

Global $IsItemChecked       = False
Global $LV_CHECKED_INDEX    = -1

$GUI = GUICreate("ListView - Check All CheckBoxes Demo", 300, 240)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")

$ListView = GUICtrlCreateListView("Column", 20, 10, 260, 220)
_GUICtrlListView_SetExtendedListViewStyle($ListView, $LVS_EX_CHECKBOXES)

GUICtrlCreateListViewItem("Check all Items", $ListView)

For $i = 1 To 10
    GUICtrlCreateListViewItem("Item " & $i, $ListView)
Next

GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, -1)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY_EVENTS")

GUISetState(@SW_SHOW, $GUI)

While 1
    Sleep(10)
    
    If $IsItemChecked Then
        $IsItemChecked = False
        
        $iItemsCount = _GUICtrlListView_GetItemCount($ListView)
        $iCheckedState = _GUICtrlListView_GetItemChecked($ListView, $LV_CHECKED_INDEX)
        
        If $LV_CHECKED_INDEX = 0 Then
            For $i = 1 To $iItemsCount-1
                _GUICtrlListView_SetItemChecked($ListView, $i, $iCheckedState)
            Next
        Else
            If _AllItemsIsChecked($ListView, 1) Then _GUICtrlListView_SetItemChecked($ListView, 0, 1)
            If _AllItemsIsUnChecked($ListView, 1) Then _GUICtrlListView_SetItemChecked($ListView, 0, 0)
        EndIf
    EndIf
WEnd

Func _AllItemsIsChecked($hLV, $iStartIndex=0)
    For $i = $iStartIndex To $iItemsCount-1
        If _GUICtrlListView_GetItemChecked($hLV, $i) = 0 Then Return False
    Next
    
    Return True
EndFunc

Func _AllItemsIsUnChecked($hLV, $iStartIndex=0)
    For $i = $iStartIndex To $iItemsCount-1
        If _GUICtrlListView_GetItemChecked($hLV, $i) = 1 Then Return False
    Next
    
    Return True
EndFunc

Func WM_NOTIFY_EVENTS($hWndGUI, $MsgID, $wParam, $lParam)
    Local $stTagNMHDR = DllStructCreate($tagNMHDR, $lParam)
    If @error Then Return $GUI_RUNDEFMSG
    If $wParam <> $ListView Then Return $GUI_RUNDEFMSG
    
    Local $iCode = DllStructGetData($stTagNMHDR, "Code")
    
    Switch $iCode
        Case $LVN_ITEMCHANGING
            
        Case $LVN_ITEMCHANGED
            
        Case $NM_CLICK, $NM_DBLCLK
            Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
            Local $iIndex = DllStructGetData($tInfo, "Index")
            
            If $iIndex <> -1 Then
                $LV_CHECKED_INDEX = $iIndex
                
                Local $iX = DllStructGetData($tInfo, "X")
                Local $aIconRect = _GUICtrlListView_GetItemRect($ListView, $iIndex, 1)
                
                If $iX < $aIconRect[0] And $iX >= 5 And $iX < 21 Then
                    $IsItemChecked = True
                    
                    If @error Then Return 1
                    Return 0
                EndIf
            EndIf
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc

Func Quit()
    Exit
EndFunc

Next time better post what you have tried, it's will increes your chances to get help :)

Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

I would like to have only one column in the listview. Is that possible?

Hm... in my example there only one column... if you talking about the seperater after that column, then you need to set width for the column, so the seperater will be equel to the ListView width..

Just replace this:

GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, -1)

with this:

GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, 256)

256 can be changed, depends on system metrics i think.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Or, you can use Group instead of columns...

#include <GuiConstants.au3>
#include <GuiListView.au3>
Opt("GuiOnEventMode", 1)

Global $IsItemChecked       = False
Global $LV_CHECKED_INDEX    = -1

$GUI = GUICreate("ListView - Check All CheckBoxes Demo", 300, 240)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")

$ListView = GUICtrlCreateListView("Items", 20, 10, 260, 220, $LVS_NOCOLUMNHEADER)
_GUICtrlListView_SetExtendedListViewStyle($ListView, $LVS_EX_CHECKBOXES)

_GUICtrlListView_EnableGroupView($ListView)
_GUICtrlListView_InsertGroup($ListView, -1, 0, "Items")
_GUICtrlListView_SetItemGroupID($ListView, -1, 0)

GUICtrlCreateListViewItem("Check all Items", $ListView)

For $i = 1 To 5
    GUICtrlCreateListViewItem("Item " & $i, $ListView)
Next

GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, -1)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY_EVENTS")

GUISetState(@SW_SHOW, $GUI)

While 1
    Sleep(10)
    
    If $IsItemChecked Then
        $IsItemChecked = False
        
        $iItemsCount = _GUICtrlListView_GetItemCount($ListView)
        $iCheckedState = _GUICtrlListView_GetItemChecked($ListView, $LV_CHECKED_INDEX)
        
        If $LV_CHECKED_INDEX = 0 Then
            For $i = 1 To $iItemsCount-1
                _GUICtrlListView_SetItemChecked($ListView, $i, $iCheckedState)
            Next
        Else
            If _AllItemsIsChecked($ListView, 1) Then _GUICtrlListView_SetItemChecked($ListView, 0, 1)
            If _AllItemsIsUnChecked($ListView, 1) Then _GUICtrlListView_SetItemChecked($ListView, 0, 0)
        EndIf
    EndIf
WEnd

Func _AllItemsIsChecked($hLV, $iStartIndex=0)
    For $i = $iStartIndex To $iItemsCount-1
        If _GUICtrlListView_GetItemChecked($hLV, $i) = 0 Then Return False
    Next
    
    Return True
EndFunc

Func _AllItemsIsUnChecked($hLV, $iStartIndex=0)
    For $i = $iStartIndex To $iItemsCount-1
        If _GUICtrlListView_GetItemChecked($hLV, $i) = 1 Then Return False
    Next
    
    Return True
EndFunc

Func WM_NOTIFY_EVENTS($hWndGUI, $MsgID, $wParam, $lParam)
    Local $stTagNMHDR = DllStructCreate($tagNMHDR, $lParam)
    If @error Then Return $GUI_RUNDEFMSG
    If $wParam <> $ListView Then Return $GUI_RUNDEFMSG
    
    Local $iCode = DllStructGetData($stTagNMHDR, "Code")
    
    Switch $iCode
        Case $LVN_ITEMCHANGING
            
        Case $LVN_ITEMCHANGED
            
        Case $NM_CLICK, $NM_DBLCLK
            Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
            Local $iIndex = DllStructGetData($tInfo, "Index")
            
            If $iIndex <> -1 Then
                $LV_CHECKED_INDEX = $iIndex
                
                Local $iX = DllStructGetData($tInfo, "X")
                Local $aIconRect = _GUICtrlListView_GetItemRect($ListView, $iIndex, 1)
                
                If $iX < $aIconRect[0] And $iX >= 5 And $iX < 21 Then
                    $IsItemChecked = True
                    
                    If @error Then Return 1
                    Return 0
                EndIf
            EndIf
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc

Func Quit()
    Exit
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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...