Jump to content

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:

http://youtu.be/V1yEsU6oQ6s

 

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
Link to comment
Share on other sites

Edit2:

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)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

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

GUISetState(@SW_SHOW, $hGUI)
#endregion GUI

Local $aText = 0

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
    Sleep(10)
WEnd

GUIDelete($hGUI)

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)
                            EndIf

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

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

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Notes:

-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
Link to comment
Share on other sites

  • 2 years later...

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...