Juno_okyo

Listview + checkboxes

Dec 10th, 2014
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.01 KB | None | 0 0
  1. #NoTrayIcon
  2. #include <GUIConstantsEx.au3>
  3. #include <GUIListView.au3>
  4. #include <WindowsConstants.au3>
  5. #include <ButtonConstants.au3>
  6. #include <GUIButton.au3>
  7.  
  8. Example()
  9.  
  10. Func Example()
  11.     Local Const $hGUI = GUICreate("", 400, 300, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))
  12.     Local Const $iListView = GUICtrlCreateListView("", 0, 0, 400, 270)
  13.     Local Const $hListView = GUICtrlGetHandle($iListView)
  14.     $hlistviewHeader = _GUICtrlListView_GetHeader($hlistview)
  15.     $chk = _GUICtrlButton_Create($hlistviewHeader, "", 0, 0, 20, 20, $BS_AUTOCHECKBOX)
  16.     _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
  17.     GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
  18.  
  19.     Local $iSelectionState = GUICtrlCreateButton("Change State", 400 - 90, 275, 85, 22.5)
  20.     GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKSIZE + $GUI_DOCKBOTTOM)
  21.  
  22.     GUISetState(@SW_SHOWNORMAL, $hGUI)
  23.  
  24.     __ListViewFill($hListView, Random(1, 5, 1), Random(2, 15, 1), 1) ; Randomly fill data in a ListView.
  25.  
  26.     While 1
  27.         Switch GUIGetMsg()
  28.             Case $GUI_EVENT_CLOSE
  29.                 ExitLoop
  30.  
  31.             Case $iSelectionState
  32.                 _GUICtrlListView_SetCheckedStates($hListView, 2)
  33.                 MsgBox(4096, "", "All items were inverted, so if an item was checked before it was unchecked afterwards.")
  34.  
  35.                 _GUICtrlListView_SetCheckedStates($hListView, 0)
  36.                 MsgBox(4096, "", "All items were unchecked.")
  37.  
  38.                 _GUICtrlListView_SetCheckedStates($hListView, 1)
  39.                 MsgBox(4096, "", "All items were checked.")
  40.         EndSwitch
  41.     WEnd
  42.  
  43.     Return GUIDelete($hGUI)
  44. EndFunc   ;==>Example
  45.  
  46. ;~ $iType: 0 - UnCheck all, 1 - Check all & 2 - Invert selection.
  47. Func _GUICtrlListView_SetCheckedStates(Const $hListView, Const $iType) ; By Zedna, Modified by guinness.
  48.     Local $fState = False
  49.  
  50.     Local Const $iCount = _GUICtrlListView_GetItemCount($hListView)
  51.  
  52.     If $iType < 0 Or $iType > 2 Then
  53.         Return SetError(1, 0, 0)
  54.     EndIf
  55.  
  56.     If $iType Then
  57.         $fState = True
  58.     EndIf
  59.  
  60.     For $i = 0 To $iCount - 1
  61.         If $iType = 2 Then
  62.             $fState = Not _GUICtrlListView_GetItemChecked($hListView, $i) ; Invert checked state with $iType 2.
  63.         EndIf
  64.  
  65.         _GUICtrlListView_SetItemChecked($hListView, $i, $fState)
  66.     Next
  67. EndFunc   ;==>_GUICtrlListView_SetCheckedStates
  68.  
  69. Func __ListViewFill(Const $hListView, Const $iColumns, Const $iRows, Const $iCheckboxes = 0) ; Randomly fill data in a ListView.
  70.     For $A = 0 To $iColumns - 1
  71.         _GUICtrlListView_InsertColumn($hListView, $A, "Column " & $A + 1, 50)
  72.         _GUICtrlListView_SetColumnWidth($hListView, $A - 1, -2)
  73.     Next
  74.  
  75.     For $A = 0 To $iRows - 1
  76.         _GUICtrlListView_AddItem($hListView, Chr(Random(65, 90, 1)) & " - Row " & $A + 1 & ": Col 1", $A)
  77.  
  78.         If Random(0, 1, 1) And $iCheckboxes Then
  79.             _GUICtrlListView_SetItemChecked($hListView, $A)
  80.         EndIf
  81.  
  82.         For $B = 1 To $iColumns
  83.             _GUICtrlListView_AddSubItem($hListView, $A, "Row " & $A + 1 & ": Col " & $B + 1, $B)
  84.         Next
  85.     Next
  86. EndFunc   ;==>__ListViewFill
Add Comment
Please, Sign In to add comment