Sign in to follow this  
Followers 0
ASPenLink

Listview With Multiple Columns of Checkboxes

6 posts in this topic

I am trying to create a listview with multiple columns of Checkboxes. I am pulling data from a database to populate the list view. Ultimately, I want the user to be able to check and uncheck several columns in each row and then save the data back to the database.

Here is some code I pulled from another post that allows the user to edit text within a listview. I would like to change the columns SubItem1 and SubItem2 into checkboxes (instead of text). I have searched the forums, but I can't find any examples of using more than one checkbox within a listview.

Thanks,

ASPenLink

#include <GuiEdit.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GuiListView.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <DateTimeConstants.au3>
#include <GuiDateTimePicker.au3>
#include <Date.au3>

Opt("GuiCloseOnESC", 0)

Global $hEdit, $hDC, $hBrush, $Item = -1, $SubItem = 0

Global $Style = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)

$hGUI = GUICreate("ListView Subitems edit in place", 300, 200)

$hListView = _GUICtrlListView_Create($hGUI, "Items|SubItem1|SubItem2", 2, 2, 296, 196, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_GRIDLINES)

For $i = 1 To 11
    _GUICtrlListView_AddItem($hListView, "Item " & $i)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem1- " & $i, 1)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem2- " & $i, 2)
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR, $hWndFrom, $iCode

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
   
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_DBLCLK
                    Local $aHit = _GUICtrlListView_SubItemHitTest($hListView)
                    If ($aHit[0] <> -1) And ($aHit[1] > 0) Then
                        $Item = $aHit[0]
                        $SubItem = $aHit[1]
                        Local $iSubItemText = _GUICtrlListView_GetItemText($hListView, $Item, $SubItem)
                        Local $iLen = _GUICtrlListView_GetStringWidth($hListView, $iSubItemText)
                        Local $aRect = _GUICtrlListView_GetSubItemRect($hListView, $Item, $SubItem)
                        $hEdit = _GUICtrlEdit_Create($hGUI, $iSubItemText, $aRect[0] + 6, $aRect[1], $iLen + 10, 17, $Style)
                        _GUICtrlEdit_SetSel($hEdit, 0, -1)
                        _WinAPI_SetFocus($hEdit)
                        $hDC = _WinAPI_GetWindowDC($hEdit)
                        $hBrush = _WinAPI_CreateSolidBrush(0)
                        FrameRect($hDC, 0, 0, $iLen + 10 , 17, $hBrush)
                        
                    EndIf
            EndSwitch
    EndSwitch
       
    Return $GUI_RUNDEFMSG
EndFunc

Func FrameRect($hDC, $nLeft, $nTop, $nRight, $nBottom, $hBrush)
    Local $stRect = DllStructCreate("int;int;int;int")
    
    DllStructSetData($stRect, 1, $nLeft)
    DllStructSetData($stRect, 2, $nTop)
    DllStructSetData($stRect, 3, $nRight)
    DllStructSetData($stRect, 4, $nBottom)
    
    DllCall("user32.dll", "int", "FrameRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "hwnd", $hBrush)
EndFunc

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $iCode = BitShift($wParam, 16)
   
    Switch $lParam
        Case $hEdit
            Switch $iCode
                Case $EN_KILLFOCUS
                    Local $iText = _GUICtrlEdit_GetText($hEdit)
                    _GUICtrlListView_SetItemText($hListView, $Item, $iText, $SubItem)
                    _WinAPI_DeleteObject($hBrush)
                    _WinAPI_ReleaseDC($hEdit, $hDC)
                    _WinAPI_DestroyWindow($hEdit)
                    
                    $Item = -1
                    $SubItem = 0
            EndSwitch
    EndSwitch
   
    Return $GUI_RUNDEFMSG
EndFunc

Share this post


Link to post
Share on other sites



I am trying to create a listview with multiple columns of Checkboxes. I am pulling data from a database to populate the list view. Ultimately, I want the user to be able to check and uncheck several columns in each row and then save the data back to the database.

Here is some code I pulled from another post that allows the user to edit text within a listview. I would like to change the columns SubItem1 and SubItem2 into checkboxes (instead of text). I have searched the forums, but I can't find any examples of using more than one checkbox within a listview.

Thanks,

ASPenLink

This is a work around. Food for thought if nothing better comes along.

#include <GuiEdit.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GuiListView.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <DateTimeConstants.au3>
#include <GuiDateTimePicker.au3>
#include <Date.au3>

Opt("GuiCloseOnESC", 0)

Global $hEdit, $hDC, $hBrush, $Item = -1, $SubItem = 0,$n

Global $Style = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)

$hGUI = GUICreate("ListView Subitems edit in place", 300, 200)
GUISetBkColor (0xFFFFFF)
$hListView = _GUICtrlListView_Create($hGUI, "SubItem1|SubItem2", 58, 2, 296, 196, BitOR($LVS_EDITLABELS, $LVS_REPORT))
_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_GRIDLINES)

$n = 17 ; Y  Position value start of CheckBox
dim $aItemColumn[12] ; array of identifier (controlID) of CheckBoxes
GUICtrlCreateLabel(" Item",5,3,53,20)
GUICtrlSetBkColor (-1,0xD4D0C8)

For $i = 1 To 11
    $aItemColumn[$i] = GUICtrlCreateCheckbox("CHk" & $i, 5 , $n, 52, 17,-1,$WS_EX_TOPMOST )
    GUICtrlSetBkColor (-1,0xFFFFFF) 
    _GUICtrlListView_AddItem($hListView,"")
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem1- " & $i, 1)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem2- " & $i, 2)
    $n += 14 ;Increment Y Position value of Checkbox
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR, $hWndFrom, $iCode

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
  
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_DBLCLK
                    Local $aHit = _GUICtrlListView_SubItemHitTest($hListView)
                    If ($aHit[0] <> -1) And ($aHit[1] > 0) Then
                        $Item = $aHit[0]
                        $SubItem = $aHit[1]
                        Local $iSubItemText = _GUICtrlListView_GetItemText($hListView, $Item, $SubItem)
                        Local $iLen = _GUICtrlListView_GetStringWidth($hListView, $iSubItemText)
                        Local $aRect = _GUICtrlListView_GetSubItemRect($hListView, $Item, $SubItem)
                        $hEdit = _GUICtrlEdit_Create($hGUI, $iSubItemText, $aRect[0] + 6, $aRect[1], $iLen + 10, 17, $Style)
                        _GUICtrlEdit_SetSel($hEdit, 0, -1)
                        _WinAPI_SetFocus($hEdit)
                        $hDC = _WinAPI_GetWindowDC($hEdit)
                        $hBrush = _WinAPI_CreateSolidBrush(0)
                        FrameRect($hDC, 0, 0, $iLen + 10 , 17, $hBrush)
                        
                    EndIf
            EndSwitch
    EndSwitch
      
    Return $GUI_RUNDEFMSG
EndFunc

Func FrameRect($hDC, $nLeft, $nTop, $nRight, $nBottom, $hBrush)
    Local $stRect = DllStructCreate("int;int;int;int")
    
    DllStructSetData($stRect, 1, $nLeft)
    DllStructSetData($stRect, 2, $nTop)
    DllStructSetData($stRect, 3, $nRight)
    DllStructSetData($stRect, 4, $nBottom)
    
    DllCall("user32.dll", "int", "FrameRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "hwnd", $hBrush)
EndFunc

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $iCode = BitShift($wParam, 16)
  
    Switch $lParam
        Case $hEdit
            Switch $iCode
                Case $EN_KILLFOCUS
                    Local $iText = _GUICtrlEdit_GetText($hEdit)
                    _GUICtrlListView_SetItemText($hListView, $Item, $iText, $SubItem)
                    _WinAPI_DeleteObject($hBrush)
                    _WinAPI_ReleaseDC($hEdit, $hDC)
                    _WinAPI_DestroyWindow($hEdit)
                    
                    $Item = -1
                    $SubItem = 0
            EndSwitch
    EndSwitch
  
    Return $GUI_RUNDEFMSG
EndFunc

Changing the X positioning values of the CheckBox & ListView, you should be able to rearrange to the way you want.

It's a reply. Probably not the one you wanted.

If you could provide the link to the checkbox within a listview, I'd like to see how it was done.

Share this post


Link to post
Share on other sites

Thanks for the reply, but I really need to have more than one column of checkboxes. Perhaps someone else will have some ideas.

Thanks,

ASPenLink

Share this post


Link to post
Share on other sites

Malkey,

Per your request, here is the sample of a listview using a single checkbox. However, I'm still looking for multiple columns of checkboxes.

Enjoy!

ASPenLink

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <Misc.au3>

Opt("GuiOnEventMode", 1)

Global $DllHandle = DllOpen("user32.dll")

$hGUI = GUICreate("Test GUI", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$ListView = GUICtrlCreateListView("Items|SubItems", 10, 10, 280, 180, -1, BitOR($LVS_EX_CHECKBOXES, $WS_EX_CLIENTEDGE))

For $i = 1 To 10
    GUICtrlCreateListViewItem("Item " & $i & "|SubItem " & $i, $ListView)
    GUICtrlSetOnEvent(-1, "_GetChecked")
Next

GUISetState()

While 1
    If _IsPressed("20", $DllHandle) Then
        _Check()
        Sleep(100)
    EndIf
    
    Sleep(100)
WEnd

Func _Exit()
    DllClose($DllHandle)
    Exit
EndFunc

Func _GetChecked()
    If GUICtrlRead(@GUI_CtrlId, 1) = 1 Then ConsoleWrite(GUICtrlRead(@GUI_CtrlId) & " is checked" & @LF)
EndFunc

Func _Check()
    Local $iItem = _GUICtrlListView_GetSelectedIndices($ListView)
    If $iItem = "" Then Return False
    
    If _GUICtrlListView_GetItemChecked($ListView, $iItem) Then ConsoleWrite("Item " & $iItem + 1 & " is checked" & @LF)
EndFunc

Share this post


Link to post
Share on other sites

So, what?

Is there a way (udf?) to get multiple checkboxes in a single entry of a listview?

Maybe I used wrong search parameters, but I'm not able to find a hint for that issue here in the forum.

It would very nice, if someone could point out: Yes or no.

In case listview is the wrong approach, just most your suggestion.

Thank you

Share this post


Link to post
Share on other sites

Tankbuster

Is there a way (udf?) to get multiple checkboxes in a single entry of a listview?

Search in the Google

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  
Followers 0