Jump to content
Sign in to follow this  

Checking a checkbox on uncheck (in listview)

Recommended Posts

I searched around for a bit before asking this, so please bear with me.


Currently I have a program where only one checkbox in a list can be selected, but I want to make it so that it;s impossible to have zero boxes checked.

For example if user unchecks a box, it should re-check itself. (I am trying to get a radio-option type effect with checkboxes in a listview)


See below:



I want to make it so one box is always checked. How should I go about this? Thanks as always for your time. (:

It doesn't actually even have to re-check the same box that was un-checked, as long as it checks a box when one is unchecked.

Edited by nullschritt

Share this post

Link to post
Share on other sites


Final example (and completed notes) :

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

Global Const $LVM_CHECKED = 8192, $LVM_UNCHECKED = 4096

Global $iLastItemChecked = -1, $blItemAdded = False

#region GUI
Local $hGUI = GUICreate("MyGUI", 500, 500)

Global $iLw1 = GUICtrlCreateListView("Column1|Column2", 10, 10, 480, 250, Default, $LVS_EX_CHECKBOXES)


For $i = 0 To 9
    $blItemAdded = True
    GUICtrlCreateListViewItem("Item " & $i, $iLw1)
_GUICtrlListView_SetItemChecked($iLw1, 1)
$iLastItemChecked = 1

GUISetState(@SW_SHOW, $hGUI)
#endregion GUI

Local $aText = 0

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $iIDFrom = 0, $iCode = 0, $tNMHDR = 0, $tInfo = 0

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $iIDFrom
        Case $iLw1
            Switch $iCode
                Case $LVN_ITEMCHANGED
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    $iNewState = DllStructGetData($tInfo, "NewState")

                    Switch $iNewState
                        Case $LVM_CHECKED
                            If $iLastItemChecked <> -1 Then
                                _GUICtrlListView_SetItemChecked($iLw1, $iLastItemChecked, False)

                            $iLastItemChecked = DllStructGetData($tInfo, "Item")
                        Case $LVM_UNCHECKED
                            If $blItemAdded Then
                                $blItemAdded = False
                                Return $GUI_RUNDEFMSG

                            _GUICtrlListView_SetItemChecked($iLw1, DllStructGetData($tInfo, "Item")) ;re-check the unchecked item

EndFunc   ;==>WM_NOTIFY

-I replaced the code $LVN_ITEMCHANGING to the code $LVN_ITEMCHANGED, otherwise the checkbox is not yet unchecked (and can not be automatically re-checked).

-I added the $blItemAdded var, otherwise the item created is checked (because on its creation the $LVM_UNCHECKED status is called).

If you want to create/insert other items, don't forget to update this last to true.

Br, FireFox.

Edited by FireFox


OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control


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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...