Jump to content

Uncheck check boxes when a radio button is selected


Recommended Posts

Hi,

I'm still a novice when it comes to AutoIT, as I only dabble with it every now and then.

I want to try and write a script that I can use as a front end for USMT (the Windows profile migrating tool).

My script is still in it's very early stages, but I cannot seem to make the user list checkboxes uncheck themselves (if any are checked) when the "All Users" radio button is selected.

All that happens is the first name in the list is unchecked (if you click the "Backup" button, which I've just used as a trigger.  Ideally I want to have all check boxes cleared when the "All Users" radio button is selected.) 

I'm sure my script could be written in a far more elegant way, but here it is so far, if anyone can tell me where I'm going wrong I'd be most happy!

#RequireAdmin
#include <GUIConstantsEx.au3>
#include <WinAPIFiles.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <ListViewConstants.au3>
#Include <File.au3>
#Include <Constants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 692, 411, 531, 349)
GUISetBkColor(0xA6CAF0)

;StorePath input box
$StorePathLabel = GUICtrlCreateLabel("Storepath:", 18, 36, 53, 17)
$StorePath = GUICtrlCreateInput("<PATH TO MIG FILE>", 75, 33, 441, 21)

;Browse button
$Button1 = GUICtrlCreateButton("Browse", 528, 15, 145, 57)

;Line around Users group
$UsersGroup = GUICtrlCreateGroup("User(s)", 406, 84, 265, 305)

$AllUsersRadio = GUICtrlCreateRadio("All Users", 422, 113, 65, 17)
GUICtrlSetState ( $AllUsersRadio, $GUI_CHECKED )
$DaysCheckBox = GUICtrlCreateCheckbox("Users logged in within last", 452, 141, 142, 17)
$DaysNumber = GUICtrlCreateInput("30", 594, 140, 33, 21, $ES_CENTER)
GUICtrlSetLimit(-1, 3)

;Draw corner box
$Graphic1 = GUICtrlCreateGraphic(422, 130, 26, 26)
GUICtrlSetGraphic(-1, $GUI_GR_PENSIZE, 2)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x000000)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 6, 5)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 6, 20)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 5, 20)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 20, 20)

$DaysLabel = GUICtrlCreateLabel("days", 630, 143, 26, 17)

;Selected Users box
$SelectedUsersRadio = GUICtrlCreateRadio("Selected Users", 423, 180, 113, 17)
$ListProfiles = GUICtrlCreateListView( "", 416, 200, 247, 180, BitOR($LVS_DEFAULT, $LVS_NOCOLUMNHEADER) )
GUICtrlSetState ( $ListProfiles, $GUI_DISABLE)
_GUICtrlListView_SetExtendedListViewStyle( $ListProfiles, $LVS_EX_CHECKBOXES )
_GUICtrlListView_InsertColumn ( $ListProfiles, 0, "", 120 )
_GUICtrlListView_InsertColumn ( $ListProfiles, 1, "", 70 )
$ProfileList = _FileListToArray("C:\Users\", "*", 2)
Global $ProfileSize[ubound($ProfileList)]
For $i = 1 to ubound($ProfileList) - 1
    _GUICtrlListview_AddItem($ListProfiles,$ProfileList[$i], 100)
    $ProfileSize[$i] = DirGetSize ( "C:\Users\" & $ProfileList[$i] )
    $ProfileSize[$i] = Round ( $ProfileSize[$i]/1024/1024/1024,2 )
    If $ProfileSize[$i] = "" Then
        _GUICtrlListView_AddSubItem($ListProfiles, ( $i - 1 ), $ProfileSize[$i], 1)
    Else
        _GUICtrlListView_AddSubItem($ListProfiles, ( $i - 1 ), $ProfileSize[$i] & " GB", 1)
    EndIf
Next

$Backup = GUICtrlCreateButton("Backup", 19, 91, 374, 134)
GUICtrlSetFont(-1, 24, 400, 0, "Arial")
$Restore = GUICtrlCreateButton("Restore", 20, 256, 374, 134)
GUICtrlSetFont(-1, 24, 400, 0, "Arial")

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

        Case $AllUsersRadio
            GUICtrlSetState ( $ListProfiles, $GUI_DISABLE )

        Case $SelectedUsersRadio
            GUICtrlSetState ( $ListProfiles, $GUI_ENABLE )

        Case $Backup
            For $i = 1 to UBound($ProfileList) - 1
            _GUICtrlListview_SetItemChecked($ListProfiles,$ProfileList[$i], False)
            Next

        Case $Restore
            MsgBox ( "", "", "Restore button clicked", 0.5 )

    EndSwitch
WEnd

 

Link to comment
Share on other sites

Test this script:

;#RequireAdmin
#include <GUIConstantsEx.au3>
#include <WinAPIFiles.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <ListViewConstants.au3>
#include <File.au3>
#include <Constants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("Form1", 692, 411, 531, 349)
GUISetBkColor(0xA6CAF0)
;StorePath input box
$StorePathLabel = GUICtrlCreateLabel("Storepath:", 18, 36, 53, 17)
$StorePath = GUICtrlCreateInput("<PATH TO MIG FILE>", 75, 33, 441, 21)

;Browse button
$Button1 = GUICtrlCreateButton("Browse", 528, 15, 145, 57)

;Line around Users group
$UsersGroup = GUICtrlCreateGroup("User(s)", 406, 84, 265, 305)

$AllUsersRadio = GUICtrlCreateRadio("All Users", 422, 113, 65, 17)
GUICtrlSetState($AllUsersRadio, $GUI_CHECKED)
$DaysCheckBox = GUICtrlCreateCheckbox("Users logged in within last", 452, 141, 142, 17)
$DaysNumber = GUICtrlCreateInput("30", 594, 140, 33, 21, $ES_CENTER)
GUICtrlSetLimit(-1, 3)

;Draw corner box
$Graphic1 = GUICtrlCreateGraphic(422, 130, 26, 26)
GUICtrlSetGraphic(-1, $GUI_GR_PENSIZE, 2)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x000000)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 6, 5)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 6, 20)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 5, 20)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 20, 20)

$DaysLabel = GUICtrlCreateLabel("days", 630, 143, 26, 17)

;Selected Users box
$SelectedUsersRadio = GUICtrlCreateRadio("Selected Users", 423, 180, 113, 17)
$ListProfiles = GUICtrlCreateListView("", 416, 200, 247, 180, BitOR($LVS_DEFAULT, $LVS_NOCOLUMNHEADER))
GUICtrlSetState ( $ListProfiles, $GUI_DISABLE)
_GUICtrlListView_SetExtendedListViewStyle($ListProfiles, $LVS_EX_CHECKBOXES)
_GUICtrlListView_InsertColumn($ListProfiles, 0, "", 120)
_GUICtrlListView_InsertColumn($ListProfiles, 1, "", 70)
$ProfileList = _FileListToArray("C:\Users\", "*", 2)
_ArrayColInsert($ProfileList, 1) ;Size
_ArrayColInsert($ProfileList, 2) ;Selected?
;yet, $ProfileList is a 2D Array
_ArrayDisplay($ProfileList)

For $i = 1 To UBound($ProfileList) - 1
    _GUICtrlListView_AddItem($ListProfiles, $ProfileList[$i][0], 100)
    $ProfileList[$i][1] = DirGetSize("C:\Users\" & $ProfileList[$i][0])
    $ProfileList[$i][1] = Round($ProfileList[$i][1] / 1024 / 1024 / 1024, 2)
    If $ProfileList[$i][1] = "" Then
        _GUICtrlListView_AddSubItem($ListProfiles, ($i - 1), $ProfileList[$i][1], 1)
    Else
        _GUICtrlListView_AddSubItem($ListProfiles, ($i - 1), $ProfileList[$i][1] & " GB", 1)
    EndIf
Next

$Backup = GUICtrlCreateButton("Backup", 19, 91, 374, 134)
GUICtrlSetFont(-1, 24, 400, 0, "Arial")
$Restore = GUICtrlCreateButton("Restore", 20, 256, 374, 134)
GUICtrlSetFont(-1, 24, 400, 0, "Arial")

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

        Case $AllUsersRadio
            GUICtrlSetState($ListProfiles, $GUI_DISABLE)
            For $i = 1 To UBound($ProfileList) - 1
                $ProfileList[$i][2]=_GUICtrlListView_GetItemChecked($ListProfiles,$i-1)
                _GUICtrlListView_SetItemChecked($ListProfiles, ($i - 1), False)
            Next
            _ArrayDisplay($ProfileList)
        Case $SelectedUsersRadio
            GUICtrlSetState($ListProfiles, $GUI_ENABLE)
            For $i = 1 To UBound($ProfileList) - 1
                _GUICtrlListView_SetItemChecked($ListProfiles, ($i - 1), $ProfileList[$i][2]) ;restore Checkstate from array
            Next
        Case $Backup
            For $i = 1 To UBound($ProfileList) - 1
                _GUICtrlListView_SetItemChecked($ListProfiles, $ProfileList[$i], False)
                $ProfileList[$i][2]=False
            Next

        Case $Restore
            MsgBox("", "", "Restore button clicked", 0.5)

    EndSwitch
WEnd

may be is a hint which is the way to solve

Link to comment
Share on other sites

Your wrong code:

Case $Backup
  For $i = 1 to UBound($ProfileList) - 1
    _GUICtrlListview_SetItemChecked($ListProfiles,$ProfileList[$i], False)
  Next

The help file:

_GUICtrlListView_SetItemChecked ( $hWnd, $iIndex [, $bCheck = True] )
; $iIndex: 0-based index of the item, -1 sets all items

Your proper code:

Case $Backup
  For $i = 1 to UBound($ProfileList) - 1
    _GUICtrlListview_SetItemChecked($ListProfiles, $i-1, False)
  Next

Or simply:

Case $Backup
  ;For $i = 1 to UBound($ProfileList) - 1
    _GUICtrlListview_SetItemChecked($ListProfiles, -1, False)
  ;Next

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...