Snippets ( Checkboxes )

From AutoIt Wiki
Jump to navigation Jump to search

Alert2.gif Please always credit an author in your script if you use their code. It is only polite.


Are All Items Checked?

Author: guinness








#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local Const $hGUI = GUICreate("", 400, 300, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))
    Local $iListView = GUICtrlCreateListView("", 0, 0, 400, 270)
    Local Const $hListView = GUICtrlGetHandle($iListView)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
    GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)

    Local $iAllSelected = GUICtrlCreateButton("All Selected?", 400 - 90, 275, 85, 22.5)
    GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKSIZE + $GUI_DOCKBOTTOM)

    GUISetState(@SW_SHOWNORMAL, $hGUI)

    __ListViewFill($hListView, Random(1, 5, 1), Random(2, 15, 1), 1) ; Randomly fill data in a ListView.

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $iAllSelected
                MsgBox(4096, "", "If all items are checked this will Return True: " & _GUICtrlListView_AllChecked($hListView))
        EndSwitch
    WEnd

    Return GUIDelete($hGUI)
EndFunc   ;==>Example

Func _GUICtrlListView_AllChecked(Const $hListView)
    Local $iChecked = 0

	Local Const $iCount = _GUICtrlListView_GetItemCount($hListView)

    For $i = 0 To $iCount - 1
        If _GUICtrlListView_GetItemChecked($hListView, $i) Then
            $iChecked += 1
        EndIf
    Next

    Return $iChecked = $iCount ; Returns True - all items are checked or False - some are checked.
EndFunc   ;==>_GUICtrlListView_AllChecked

Func __ListViewFill(Const $hListView, Const $iColumns, Const $iRows, Const $iCheckboxes = 0) ; Randomly fill data in a ListView.
    For $A = 0 To $iColumns - 1
        _GUICtrlListView_InsertColumn($hListView, $A, "Column " & $A + 1, 50)
        _GUICtrlListView_SetColumnWidth($hListView, $A - 1, -2)
    Next

    For $A = 0 To $iRows - 1
        _GUICtrlListView_AddItem($hListView, Chr(Random(65, 90, 1)) & " - Row " & $A + 1 & ": Col 1", $A)

		If Random(0, 1, 1) And $iCheckboxes Then
            _GUICtrlListView_SetItemChecked($hListView, $A)
        EndIf

        For $B = 1 To $iColumns
            _GUICtrlListView_AddSubItem($hListView, $A, "Row " & $A + 1 & ": Col " & $B + 1, $B)
        Next
    Next
EndFunc   ;==>__ListViewFill

Return To Contents

_IsChecked

Author: Zedna




Modified: guinness





#include <GUIConstantsEx.au3>

Example()

Func Example()
    Local Const $aState[2] = [$GUI_CHECKED, $GUI_UNCHECKED]
    GUICreate('')
    Local Const $iCheckbox = GUICtrlCreateCheckbox("Checkbox Example", 10, 10, 120, 20)
    GUICtrlSetState($iCheckbox, $aState[Random(0, 1, 1)]) ; Randomise whether or not the checkbox is checked.
    GUISetState(@SW_SHOWNORMAL)

    ; Check the state of the checkbox.
    MsgBox(4096, '', "Is the checkbox checked: " & _IsChecked($iCheckbox))

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>Example

Func _IsChecked(Const $iControlID)
    Return BitAND(GUICtrlRead($iControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

Return To Contents

Listview With Checkboxes

Author: Zedna








#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local Const $hGUI = GUICreate("", 400, 300, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))
    Local Const $iListView = GUICtrlCreateListView("", 0, 0, 400, 270)
    Local Const $hListView = GUICtrlGetHandle($iListView)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
    GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)

    Local $iSelectionState = GUICtrlCreateButton("Change State", 400 - 90, 275, 85, 22.5)
    GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKSIZE + $GUI_DOCKBOTTOM)

    GUISetState(@SW_SHOWNORMAL, $hGUI)

    __ListViewFill($hListView, Random(1, 5, 1), Random(2, 15, 1), 1) ; Randomly fill data in a ListView.

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $iSelectionState
                _GUICtrlListView_SetCheckedStates($hListView, 2)
                MsgBox(4096, "", "All items were inverted, so if an item was checked before it was unchecked afterwards.")

                _GUICtrlListView_SetCheckedStates($hListView, 0)
                MsgBox(4096, "", "All items were unchecked.")

                _GUICtrlListView_SetCheckedStates($hListView, 1)
                MsgBox(4096, "", "All items were checked.")
        EndSwitch
    WEnd

    Return GUIDelete($hGUI)
EndFunc   ;==>Example

;~ $iType: 0 - UnCheck all, 1 - Check all & 2 - Invert selection.
Func _GUICtrlListView_SetCheckedStates(Const $hListView, Const $iType) ; By Zedna, Modified by guinness.
    Local $fState = False

	Local Const $iCount = _GUICtrlListView_GetItemCount($hListView)

	If $iType < 0 Or $iType > 2 Then
        Return SetError(1, 0, 0)
    EndIf

    If $iType Then
        $fState = True
    EndIf

    For $i = 0 To $iCount - 1
        If $iType = 2 Then
            $fState = Not _GUICtrlListView_GetItemChecked($hListView, $i) ; Invert checked state with $iType 2.
        EndIf

        _GUICtrlListView_SetItemChecked($hListView, $i, $fState)
    Next
EndFunc   ;==>_GUICtrlListView_SetCheckedStates

Func __ListViewFill(Const $hListView, Const $iColumns, Const $iRows, Const $iCheckboxes = 0) ; Randomly fill data in a ListView.
    For $A = 0 To $iColumns - 1
        _GUICtrlListView_InsertColumn($hListView, $A, "Column " & $A + 1, 50)
        _GUICtrlListView_SetColumnWidth($hListView, $A - 1, -2)
    Next

    For $A = 0 To $iRows - 1
        _GUICtrlListView_AddItem($hListView, Chr(Random(65, 90, 1)) & " - Row " & $A + 1 & ": Col 1", $A)

		If Random(0, 1, 1) And $iCheckboxes Then
            _GUICtrlListView_SetItemChecked($hListView, $A)
        EndIf

        For $B = 1 To $iColumns
            _GUICtrlListView_AddSubItem($hListView, $A, "Row " & $A + 1 & ": Col " & $B + 1, $B)
        Next
    Next
EndFunc   ;==>__ListViewFill

Return To Contents

Transparent Radio Button

Author: Valuater








#include <GUIConstantsEx.au3>
#include <ListBoxConstants.au3>
#include <GUIConstants.au3>

MsgBox(0x0, "", @AutoItVersion)

GUICreate("Transparency", 265, 295, @DesktopWidth/2-160, @DesktopHeight/2-90, -1, 0x00000218)
GUISetState(@SW_SHOWNORMAL)

Global $BI = GUICtrlCreatePic("C:\Temp\Backgrnd.jpg", 0, 0, 265, 295)
GUICtrlSetState(-1, $GUI_DISABLE)

;^) Make a group.
Global Const $Group = GUICtrlCreateGroup("", 10, 10, 100, 170)

;^) Radio buttons.
Global Const $radio1 = _GUICtrlCreateRadio("Radio 1", 20, 30, 80, 15, $GUI_BKCOLOR_TRANSPARENT, 0x00ff00) ; Green
Global Const $radio2 = _GUICtrlCreateRadio("Radio 2", 20, 50, 80, 15, $GUI_BKCOLOR_TRANSPARENT, 0xFFFFFF) ; White
Global Const $radio3 = _GUICtrlCreateRadio("Radio 3", 20, 70, 80, 15, $GUI_BKCOLOR_TRANSPARENT, 0xff0000) ; Red
Global Const $radio4 = _GUICtrlCreateRadio("Radio 4", 20, 90, 80, 15, $GUI_BKCOLOR_TRANSPARENT)
Global Const $radio5 = _GUICtrlCreateRadio("Radio 5", 20, 110, 80, 15, $GUI_BKCOLOR_TRANSPARENT)
Global Const $radio6 = _GUICtrlCreateRadio("Radio 6", 20, 130, 80, 15, $GUI_BKCOLOR_TRANSPARENT)
Global Const $radio7 = _GUICtrlCreateRadio("Radio 7", 20, 150, 80, 15, $GUI_BKCOLOR_TRANSPARENT)


;^) Buttonsf
Global Const $Butt1 = GUICtrlCreateButton("One", 10, 263, 50, 20)
Global Const $Butt2 = GUICtrlCreateButton("Two", 75, 263, 50, 20)
Global Const $Butt3 = GUICtrlCreateButton("Three", 140, 263, 50, 20)
Global Const $Butt4 = GUICtrlCreateButton("Four", 205, 263, 50, 20)

;^) List with label
GUICtrlCreateLabel("List", 130, 10, 80, 15)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
Global Const $List = GUICtrlCreateList("", 120, 25, 135, 227,$LBS_NoIntegralHeight)

;^) GUI is done
While GUIGetMsg() <> -3
Wend

Func _GUICtrlCreateRadio(Const $rText, Const $rLeft, Const $rTop, Const $rLength, Const $rHeight, Const $rBackColor = "", Const $rTextColor = "")
    Local Const $PCRadio = GUICtrlCreateRadio("", $rLeft, $rTop, 12, 12)

	GUICtrlCreateLabel($rText, $rLeft + 15, $rTop, $rLength - 15, $rHeight)

    If $rTextColor <> "" Then GUICtrlSetColor(-1, $rTextColor)

    If $rBackColor <> "" Then GUICtrlSetBkColor(-1, $rBackColor)

    Return $PCRadio
EndFunc

Return To Contents