Snippets ( Checkboxes )

From AutoIt Wiki
Revision as of 08:44, 15 May 2012 by Chimaera (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Please always credit an author in your script if you use their code, Its only polite.

Are all items checked? ~ Author - guinness

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

Example()

Func Example()
    Local $hGUI = GUICreate("", 400, 300, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))
    Local $iListView = GUICtrlCreateListView("", 0, 0, 400, 270)
    Local $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_SHOW, $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($hListView)
    Local $iChecked = 0, $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($hListView, $iColumns, $iRows, $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 $aState[2] = [$GUI_CHECKED, $GUI_UNCHECKED]
    GUICreate('')
    Local $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_SHOW)

    ; 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($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 $hGUI = GUICreate("", 400, 300, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))
    Local $iListView = GUICtrlCreateListView("", 0, 0, 400, 270)
    Local $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_SHOW, $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($hListView, $iType) ; By Zedna, Modified by guinness.
    Local $fState = False, $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($hListView, $iColumns, $iRows, $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 ~ Authors - 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_SHOW)

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

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

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


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

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

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

Func _GUICtrlCreateRadio( $rText, $rLeft, $rTop, $rLength, $rHieght, $rBackColor = "" , $rTextColor = "" )
    Local $PCRadio = GUICtrlCreateRadio("", $rLeft, $rTop, 12, 12)
    Local $PCLabel = GUICtrlCreateLabel($rText, $rLeft + 15, $rTop, $rLength - 15, $rHieght)
    If $rTextColor <> "" Then GUICtrlSetColor(-1, $rTextColor)
    If $rBackColor <> "" Then GUICtrlSetBkColor(-1, $rBackColor)
    Return $PCRadio
EndFunc


Return To Contents