Jump to content

Set checkboxes on just certain items in a ListView control


AndyS01
 Share

Recommended Posts

I'm trying to put a check box next to just some of the items in a ListView control.  I know how to create listview controls with check boxes for ALL items, but not for just SOME of them.  I figured that the only way to do this is to put images of check boxes in the first column, then change the image when I wanted to check or uncheck the item or to change to have no check box at all.
 
An example might be:
Choose one of the following:
[] Apples
[] Oranges
In my test code below, I tried using icons of checked and unchecked images, but I couldn't get that to work.
 
;Add extra ICO files to the resources which can be used with TraySetIcon(@ScriptFullPath, 3) etc
; list of filename of the Ico files to be added, First one will have number 3, then 4 ..etc
#AutoIt3Wrapper_Res_Icon_Add=C:\checked.ico
#AutoIt3Wrapper_Res_Icon_Add=C:\unchecked.ico

Opt("GUICloseOnESC", 1) ; ESC closes GUI
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
;Opt('MustDeclareVars', 1)
OnAutoItExitRegister("ExitStageLeft_normal")
;Opt("WinTitleMatchMode", -1) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>

GUICreate("ListView Set Item Image", 220, 400)

$hListView = GUICtrlCreateListView("", 2, 2, 204, 368)

$btn1 = GUICtrlCreateButton("Apples", 5, 400 - 30, 50, 25)
GUICtrlSetOnEvent($btn1, "handle_btn1")

$btn2 = GUICtrlCreateButton("Oranges", 5 + 50, 400 - 30, 70, 25)
GUICtrlSetOnEvent($btn2, "handle_btn2")

GUISetOnEvent($GUI_EVENT_CLOSE, 'ExitStageLeft_normal') ; CLOSE events

GUISetState()

; Load images
$hImage = _GUIImageList_Create(16, 16, 5, 3)

$b1 = _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0xFF0000, 16, 16)); red
$b2 = _GUIImageList_AddIcon($hImage, "C:\checked.ico.ico")
$b3 = _GUIImageList_AddIcon($hImage, "C:\Uunchecked.ico.ico")
$b4 = _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x00FF00, 16, 16)); green

; Attach the list to the listview control
_GUICtrlListView_SetImageList($hListView, $hImage, 1)

; Add a column
_GUICtrlListView_AddColumn($hListView, "Column 1", 200)

_GUICtrlListView_AddItem($hListView, "", 5)
_GUICtrlListView_AddItem($hListView, "Choose one of the following:", 5)

$itemnum_choice1 = _GUICtrlListView_AddItem($hListView, "Apples", 5)
$itemnum_choice2 = _GUICtrlListView_AddItem($hListView, "Oranges", 5)

ConsoleWrite("+++: $b1 = " & $b1 & ", $b2 = " & $b2 & ", $b3 = " & $b3 & ", $b4 = " & $b4 & @CRLF)

; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()

Func handle_btn1()
    cycle($itemnum_choice1) ; cycle 'apples' between checked, unchecked, and no checkbox
EndFunc   ;==>handle_btn1

Func handle_btn2()
    cycle($itemnum_choice2) ; cycle 'oranges' between checked, unchecked, and no checkbox
EndFunc   ;==>handle_btn2

 ; cycle 'an item between checked, unchecked, and no checkbox
 Func cycle($itemnum)
    $p = _GUICtrlListView_GetItemParam($hListView, $itemnum)
    $p += 1
    _GUICtrlListView_SetItemParam($hListView, $itemnum, $p)

    Switch Mod($p, 3)
        Case 0
            $i = $b2 ; Checked
        Case 1
            $i = $b3 ; Unchecked
        Case 2
            $i = 5 ; no checkbox
    EndSwitch

    _GUICtrlListView_SetItemImage($hListView, $itemnum, $i)
    ConsoleWrite("+++: Item: " & $itemnum & " - " & $i & @CRLF)
EndFunc   ;==>toggle

Func ExitStageLeft_normal()
    Exit
EndFunc   ;==>ExitStageLeft_normal
Link to comment
Share on other sites

I changed the test code to not use check boxes, but instead, puts a a 'checked' or 'unchecked' image for a each item in the ListView that I want to have a check box.  It works like I would like it to work (see test code below), but this test code displays a solid green for checked, a solid red box for unchecked, and no box for 'not a check box'.

However, I want to use my icons instead of the solid boxes.  I want my icons to be inserted into the executable file so I don't have to ship the .ico files separately from my executable file.

Here's my test code:

#AutoIt3Wrapper_Res_Icon_Add=C:\checked.ico
#AutoIt3Wrapper_Res_Icon_Add=C:\unchecked.ico

Opt("WinTitleMatchMode", 1)
Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC", 1)
OnAutoItExitRegister("ExitStageLeft")

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>

GUICreate("ListView Set Item Image", 220, 400)
$hListView = GUICtrlCreateListView("", 2, 2, 204, 368)
GUICtrlCreateButton("CHECK", 5, 370, 65, 25)
GUICtrlSetOnEvent(-1, "handle_btn1")
GUICtrlCreateButton("UNCHECK", 70, 370, 65, 25)
GUICtrlSetOnEvent(-1, "handle_btn2")
GUICtrlCreateButton("NOCHECK", 135, 370, 65, 25)
GUICtrlSetOnEvent(-1, "handle_btn3")
GUISetState()

; Load images
$hImage = _GUIImageList_Create(16, 16, 5, 3)
$a1 = _GUIImageList_Add($hImage, "C:\checked.ico")
$a2 = _GUIImageList_Add($hImage, "C:\unchecked.ico")
$a3 = _GUIImageList_AddIcon($hImage, @SystemDir & "shell32.dll", 110)
$a4 = _GUIImageList_AddIcon($hImage, @SystemDir & "shell32.dll", 131)
$a5 = _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0xFF0000, 16, 16)); red
$a6 = _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x00FF00, 16, 16)); green
$a7 = _GUICtrlListView_SetImageList($hListView, $hImage, 1)

; Add columns
_GUICtrlListView_AddColumn($hListView, "Column 1", 150)

; Add items
$iconnum = -7
For $x = 0 To 12
    $iconnum += 1
    $str = "Item# " & $x & " - Icon# " & $iconnum
    $ndx = _GUICtrlListView_AddItem($hListView, $str, $iconnum)
    _GUICtrlListView_SetItemImage($hListView, $ndx, $iconnum)
Next

ConsoleWrite("+++: " & StringFormat("$a1 = %d, $a2 = %d, $a3 = %d, $a4 = %d, $a5 = %d, %a7 = %d", $a1, $a2, $a3, $a4, $a5, $a6, $a7) & @CRLF)
GUISetOnEvent($GUI_EVENT_CLOSE, 'ExitStageLeft')

; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Func ExitStageLeft()
    Exit
EndFunc   ;==>ExitStageLeft

Func handle_btn1()
    testfunc(1)
EndFunc   ;==>handle_btn1

Func handle_btn2()
    testfunc(0)
EndFunc   ;==>handle_btn2

Func handle_btn3()
    testfunc(6)
EndFunc   ;==>handle_btn3

Func testfunc($which)
    $iIndex = 10
    $iImage = $which
    $hWnd = $hListView

    $ret = _GUICtrlListView_SetItemImage($hWnd, $iIndex, $iImage)

    ConsoleWrite("+++: _GUICtrlListView_SetItemImage(" & Hex($hWnd) & ", $iIndex = " & $iIndex & ", $iImage = " & $iImage & ") returns: " & $ret & @CRLF)
EndFunc   ;==>testfunc
Link to comment
Share on other sites

You could include custom images, but it would be easier to grab the icon out of the appropriate Windows DLL file.  Then you don't have to include anything.  (I have no idea which one that is, and I've checked a few dozen so far...)

I started with the assumption that control faces would be icons within a dll.  On reflection, that seems fairly stupid.  My bad.  And I'm pretty sure it would be comctl32.dll, but they're not in there.

Anyway, you can include icon files at compile time (it's an option if you're using SciTE), or you can draw one using GDI(+) and use that for your image (I think).

Edited by Artisan
Link to comment
Share on other sites

you have to create an imagelist with all the icons you need.

_GUIImageList_Create()

.

then you set the imagelist to the listview.

.

_GUICtrlListView_SetImageList()

.

in the image list, they are indexed, and you simply use the index with listviewsetitem or how the name is.

you can pull the imagelist icons out of the own exe when you have included the icons to the resource with

.

#AutoIt3Wrapper_Res_Icon_Add=

.

before compilation.

cheers E.

Edit: click on the functions in the tags to get examples

Edit: i just saw that you already managed the imagelist part. then you only need the autoitwrapper working. you find the icons then like createicon("myexe",-5) as example. always "-" for autoit.

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Link to comment
Share on other sites

  • 2 weeks later...

I already had the 2 autoitwrapper lines at the top of the script.  I modified my test script a bit, and added the capability of entering an icon#.  As before, no numbers I plugged in worked.  Do you think you could run my script (with your icons), and see if it works for you?

I'm still looking for a dll with 'check' and 'uncheck' icons.

My check and uncheck icons show up in the list of icons embedded in the .exe file (using IcoFX).  If the icons are embedded, why can't I get to them via image list?

#AutoIt3Wrapper_Res_Icon_Add=C:\checked.ico
#AutoIt3Wrapper_Res_Icon_Add=C:\unchecked.ico

Opt('MustDeclareVars', 1)
Opt("WinTitleMatchMode", 1)
Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC", 1)
OnAutoItExitRegister("ExitStageLeft")

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>

Global $hMainWin, $iID_InputCtrl, $hListView

_Main()
Exit

Func _Main()
    Local $hImage, $a1, $a2, $a3, $a4, $a5, $a6, $a7, $iconnum, $str, $ndx

    $hMainWin = GUICreate("ListView Set Item Image", 220, 400)
    $hListView = GUICtrlCreateListView("", 2, 2, 204, 280)
    GUICtrlCreateButton("CHECK", 5, 290, 65, 25)
    GUICtrlSetOnEvent(-1, "handle_Check_btn")
    GUICtrlCreateButton("UNCHECK", 70, 290, 65, 25)
    GUICtrlSetOnEvent(-1, "handle_UnCheck_btn")
    GUICtrlCreateButton("HIDECHECK", 135, 290, 70, 25)
    GUICtrlSetOnEvent(-1, "handle_HideCheckBox_btn")
    $iID_InputCtrl = GUICtrlCreateInput("", 10, 320, 100, 25)
    GUICtrlCreateButton("Icon#", 10, 345, 65, 25)
    GUICtrlSetOnEvent(-1, "handle_btn4")
    GUISetState()

    ; Load images
    $hImage = _GUIImageList_Create(16, 16, 5, 3)

    $a1 = _GUIImageList_Add($hImage, "C:\checked.ico") ; returns -1
    $a2 = _GUIImageList_Add($hImage, "C:\unchecked.ico") ; returns -1
    $a3 = _GUIImageList_AddIcon($hImage, @SystemDir & "shell32.dll", 110) ; returns 0
    $a4 = _GUIImageList_AddIcon($hImage, @SystemDir & "shell32.dll", 131) ; returns 0
    $a5 = _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0xFF0000, 16, 16)); red ; returns 0
    $a6 = _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x00FF00, 16, 16)); green ; returns 1

    ; Attach imagelist to the ListView control
    $a7 = _GUICtrlListView_SetImageList($hListView, $hImage, 1)

    ; Add columns
    _GUICtrlListView_AddColumn($hListView, "Column 1", 150)

    ; Add items
    $iconnum = -7
    For $x = 0 To 12
        $iconnum += 1
        $str = "Item# " & $x & " - Icon# " & $iconnum
        $ndx = _GUICtrlListView_AddItem($hListView, $str, $iconnum)
        _GUICtrlListView_SetItemImage($hListView, $ndx, $iconnum)
    Next

    ; Change the text for line 10
    _GUICtrlListView_SetItem($hListView, "LINE 10", 10)

    ConsoleWrite("+++: " & StringFormat("$a1 = %d, $a2 = %d, $a3 = %d, $a4 = %d, $a5 = %d, $a6 = %d, $a7 = %d", $a1, $a2, $a3, $a4, $a5, $a6, $a7) & @CRLF)
    ConsoleWrite("+++: $a7 = " & $a7 & @CRLF)

    GUISetOnEvent($GUI_EVENT_CLOSE, 'ExitStageLeft')

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EndFunc   ;==>_Main

Func ExitStageLeft()
    Exit
EndFunc   ;==>ExitStageLeft

Func handle_Check_btn()
    testfunc(0)
EndFunc   ;==>handle_Check_btn

Func handle_UnCheck_btn()
    testfunc(1)
EndFunc   ;==>handle_UnCheck_btn

Func handle_HideCheckBox_btn()
    testfunc(2)
EndFunc   ;==>handle_HideCheckBox_btn

Func handle_btn4()
    Local $iIndex = GUICtrlRead($iID_InputCtrl)
    testfunc($iIndex)
EndFunc   ;==>handle_btn4

Func testfunc($iImageNum)
    ConsoleWrite("+++: $iImageNum = " & $iImageNum & @CRLF)
    Local $iIndex, $hWnd, $ret

    $iIndex = 10
    $hWnd = $hListView

    $ret = _GUICtrlListView_SetItemImage($hWnd, $iIndex, $iImageNum)

    ConsoleWrite("+++: _GUICtrlListView_SetItemImage(" & Hex($hWnd) & ", $iIndex = " & $iIndex & ", $iImageNum = " & $iImageNum & ") returns: " & $ret & @CRLF)
EndFunc   ;==>testfunc
Link to comment
Share on other sites

How about this?

#AutoIt3Wrapper_Res_Icon_Add=C:\checked.ico
#AutoIt3Wrapper_Res_Icon_Add=C:\unchecked.ico

Opt('MustDeclareVars', 1)
Opt("WinTitleMatchMode", 1)
Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC", 1)
OnAutoItExitRegister("ExitStageLeft")

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>

Global $hMainWin, $iID_InputCtrl, $hListView

_Main()
Exit

Func _Main()
    Local $hImage, $a1, $a2, $a3, $a4, $a5, $a6, $a7, $iconnum, $str, $ndx

    $hMainWin = GUICreate("ListView Set Item Image", 220, 400)
    $hListView = GUICtrlCreateListView("", 2, 2, 204, 280)
    GUICtrlCreateButton("CHECK", 5, 290, 65, 25)
    GUICtrlSetOnEvent(-1, "handle_Check_btn")
    GUICtrlCreateButton("UNCHECK", 70, 290, 65, 25)
    GUICtrlSetOnEvent(-1, "handle_UnCheck_btn")
    GUICtrlCreateButton("HIDECHECK", 135, 290, 70, 25)
    GUICtrlSetOnEvent(-1, "handle_HideCheckBox_btn")
    $iID_InputCtrl = GUICtrlCreateInput("", 10, 320, 100, 25)
    GUICtrlCreateButton("Icon#", 10, 345, 65, 25)
    GUICtrlSetOnEvent(-1, "handle_btn4")
    GUISetState()

    ; Load images
    $hImage = _GUIImageList_Create(16, 16, 5, 3)

    $a1 = _GUIImageList_AddIcon($hImage, @ScriptFullPath, 4) ; returns -1
    $a2 = _GUIImageList_AddIcon($hImage, @ScriptFullPath, 5) ; returns -1
    $a3 = _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 110) ; returns 0; The backslash was forgotten
    $a4 = _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 131) ; returns 0; The backslash was forgotten
    $a5 = _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0xFF0000, 16, 16)); red ; returns 0
    $a6 = _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x00FF00, 16, 16)); green ; returns 1

    ; Attach imagelist to the ListView control
    $a7 = _GUICtrlListView_SetImageList($hListView, $hImage, 1)

    ; Add columns
    _GUICtrlListView_AddColumn($hListView, "Column 1", 150)

    ; Add items
    $iconnum = -7
    For $x = 0 To 12
        $iconnum += 1
        $str = "Item# " & $x & " - Icon# " & $iconnum
        $ndx = _GUICtrlListView_AddItem($hListView, $str, $iconnum)
        _GUICtrlListView_SetItemImage($hListView, $ndx, $iconnum)
    Next

    ; Change the text for line 10
    _GUICtrlListView_SetItem($hListView, "LINE 10", 10)

    ConsoleWrite("+++: " & StringFormat("$a1 = %d, $a2 = %d, $a3 = %d, $a4 = %d, $a5 = %d, $a6 = %d, $a7 = %d", $a1, $a2, $a3, $a4, $a5, $a6, $a7) & @CRLF)
    ConsoleWrite("+++: $a7 = " & $a7 & @CRLF)

    GUISetOnEvent($GUI_EVENT_CLOSE, 'ExitStageLeft')

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EndFunc   ;==>_Main

Func ExitStageLeft()
    Exit
EndFunc   ;==>ExitStageLeft

Func handle_Check_btn()
    testfunc(0)
EndFunc   ;==>handle_Check_btn

Func handle_UnCheck_btn()
    testfunc(1)
EndFunc   ;==>handle_UnCheck_btn

Func handle_HideCheckBox_btn()
    testfunc(2)
EndFunc   ;==>handle_HideCheckBox_btn

Func handle_btn4()
    Local $iIndex = GUICtrlRead($iID_InputCtrl)
    testfunc($iIndex)
EndFunc   ;==>handle_btn4

Func testfunc($iImageNum)
    ConsoleWrite("+++: $iImageNum = " & $iImageNum & @CRLF)
    Local $iIndex, $hWnd, $ret

    $iIndex = 10
    $hWnd = $hListView

    $ret = _GUICtrlListView_SetItemImage($hWnd, $iIndex, $iImageNum)

    ConsoleWrite("+++: _GUICtrlListView_SetItemImage(" & Hex($hWnd) & ", $iIndex = " & $iIndex & ", $iImageNum = " & $iImageNum & ") returns: " & $ret & @CRLF)
EndFunc   ;==>testfunc

NOTE: You MUST compile with options! This is a must.  The icons will embed into the autoitEXE but you will not be able to use them(as far as I can tell).

Simply right-click the au3 file and select compile with Options. Then click Compile Script.

Link to comment
Share on other sites

 

 I figured that the only way to do this is to put images of check boxes in the first column, then change the image when I wanted to check or uncheck the item or to change to have no check box at all.

 

Erm... not the only way:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>

$Form1 = GUICreate("Form1", 180, 600)
$ListView1 = GUICtrlCreateListView('Item', 0, 0, 180, 600, BitOR($LVS_SHOWSELALWAYS, $LVS_SINGLESEL), BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES, $LVS_EX_DOUBLEBUFFER))
_GUICtrlListView_SetColumnWidth(GUICtrlGetHandle($ListView1), 0, 150)
For $i = 0 to 49
    Local $curIndex = _GUICtrlListView_GetItemCount(GUICtrlGetHandle($ListView1))
    _GUICtrlListView_InsertItem(GUICtrlGetHandle($ListView1), 'Item ------------ #' & $i+1)

    ;disable checkbox for:
    ;    - item @ index 4 (item #5)
    ;    - item @ index 24 (item #25)
    ;    - item @ index 34 (item #35)
    ;    - all items between index 9(#10) and 19(#20) = {10, 11, 12, 13... 19, 20}
    ;    - item @ index 48 (item #49)
    Switch $i
        Case 4, 24, 34, 9 to 19, 48
            _RemoveCheckbox(GUICtrlGetHandle($ListView1), $i)
    EndSwitch
Next
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;required for preventing checkboxes from re-apearing

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

Func _RemoveCheckbox($LVM, $nIndex)
    _GUICtrlListView_SetItemState($LVM, $nIndex, 0, $LVIS_STATEIMAGEMASK)
    _WinAPI_RedrawWindow($LVM)
EndFunc

Func WM_NOTIFY($hWndGUI, $MsgID, $WParam, $LParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tNMHDR, $hwndFrom, $code, $tInfo
    $tNMHDR = DllStructCreate($tagNMHDR, $LParam)
    $hwndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $code = DllStructGetData($tNMHDR, "Code")
    Local $hWndListView = $ListView1
    If Not IsHWnd($ListView1) Then $hWndListView = GUICtrlGetHandle($ListView1)
    Switch $hwndFrom
        Case $hWndListView
            Switch $code
                Case $NM_CLICK, $NM_DBLCLK, $NM_RCLICK, $NM_RDBLCLK
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $LParam)
                    ;preventing checkboxes from re-apearing (same conditions as above)
                    Switch DllStructGetData($tInfo, "Index")
                        Case 4, 24, 34, 9 to 19, 48
                            Return True;intercept normal return message which would cause checkbox to re-apear
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc
Edited by dragan
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

×
×
  • Create New...