Jump to content

Getting the index of a checked row.


Recommended Posts

Hello all I've reached a small problem in my coding, currently I have a listview which has checkboxes, only one checkbox may be selected at a time, and it's (supposed) to save the selected one in the database and load it as the selected item on restart of the app.

 

However my only problem is I cannot get the text of the row which is currently checked, I would guess that I would have to enumerate every row in listview or somethign of the sort until I found one that was checked, and then get the text of that row? I was wondering if there was maybe a simpler way to do this or already made function to get the at least the row index of the checked row so I can then get it's text?

Edited by nullschritt
Link to comment
Share on other sites

I added a simple button and a case in the main while, and it does the trick.
I don't know what you tried to do, or you used something else than what I gave you, whatever :)

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
 
Global Const $LVM_CHECKED = 8192, $LVM_UNCHECKED = 4096
 
Global $iLastItemChecked = -1
#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
    GUICtrlCreateListViewItem("Item " & $i, $iLw1)
Next
 
Local $iBtn1 = GUICtrlCreateButton("Button1", 9, 270, 80, 22)
 
GUISetState(@SW_SHOW, $hGUI)
#endregion GUI
 
Local $aText = 0
 
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iBtn1
            If $iLastItemChecked <> -1 Then
                $aText = _GUICtrlListView_GetItemTextArray($iLw1, $iLastItemChecked)
                ConsoleWrite($aText[1] & @CrLf)
            EndIf
    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_ITEMCHANGING
                    $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
                            $iLastItemChecked = -1
                    EndSwitch
            EndSwitch
    EndSwitch
 
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Link to comment
Share on other sites

I added a simple button and a case in the main while, and it does the trick.

I don't know what you tried to do, or you used something else than what I gave you, whatever :)

 

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
 
Global Const $LVM_CHECKED = 8192, $LVM_UNCHECKED = 4096
 
Global $iLastItemChecked = -1
#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
    GUICtrlCreateListViewItem("Item " & $i, $iLw1)
Next
 
Local $iBtn1 = GUICtrlCreateButton("Button1", 9, 270, 80, 22)
 
GUISetState(@SW_SHOW, $hGUI)
#endregion GUI
 
Local $aText = 0
 
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iBtn1
            If $iLastItemChecked <> -1 Then
                $aText = _GUICtrlListView_GetItemTextArray($iLw1, $iLastItemChecked)
                ConsoleWrite($aText[1] & @CrLf)
            EndIf
    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_ITEMCHANGING
                    $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
                            $iLastItemChecked = -1
                    EndSwitch
            EndSwitch
    EndSwitch
 
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

No, I used the exact code you gave me, the above is an exerp from it after it had been integrated among other wm_notify events for other controls....

 

Also your code doesn't work, it shows the LAST item checked, not the item currently checked.

Switch $iNewState
                        Case $LVM_CHECKED
                            If $iLastItemChecked <> -1 Then
                                _GUICtrlListView_SetItemChecked($keyslist, $iLastItemChecked, False)
                                $rows = _GUICtrlListView_GetItemCount($keyslist)
                                _SQLite_Exec(-1, "UPDATE KEYS set prim='0' WHERE prim='1';")
                                MsgBox(64, "", $rows)
                                $text = _GUICtrlListView_GetItemtextarray($keyslist, $iLastItemChecked)
                                _SQlite_Query (-1, "UPDATE KEYS set prim='1' WHERE MD5 = '"&$text[2]&"'", $hQuery)
                                MsgBox(0, $text[1], $text[2])
                            EndIf

                            $iLastItemChecked = DllStructGetData($tInfo, "Item")
                        Case $LVM_UNCHECKED
                            $iLastItemChecked = -1
                    EndSwitch
Should I perhaps be getting the checked item from a different event (on click instead of on change?) Edited by nullschritt
Link to comment
Share on other sites

Also your code doesn't work, it shows the LAST item checked, not the item currently checked.

I'm sorry but as there is only one item checked, the last item checked is the item currently checked. OR I am missing something there?

Edit: The variable name is $iLastItemChecked, but it can be $iCurrentItemChecked until there is another one checked.

Edit2: To be more clear, it depends where you are using the variable.

-If you are using it in the WM_NOTIFY func before this line :

$iLastItemChecked = DllStructGetData($tInfo, "Item")

then it's the LAST item checked, which will be unchecked.

-If you are using it at/after this line, it's the CURRENT item checked.

So the var name should be $iCurrentItemChecked, maybe that's why it confused you.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

I'm sorry but as there is only one item checked, the last item checked is the item currently checked. OR I am missing something there?

 

Edit: The variable name is $iLastItemChecked, but it can be $iCurrentItemChecked until there is another one checked.

Please look at the video below, it shows the code I currently posted in action, perhaps it will help you understand the error I am experiencing, you will note the message box is displaying 

http://www.youtube.com/watch?v=IPwx3hLdArA

Edited by nullschritt
Link to comment
Share on other sites

As I said in my "Edit2" :

Switch $iNewState
    Case $LVM_CHECKED
        If $iLastItemChecked <> -1 Then
            ;LAST item checked
            _GUICtrlListView_SetItemChecked($iLw1, $iLastItemChecked, False)
            
            ;the code you placed here will be executed for the LAST item checked
        EndIf
 
        $iLastItemChecked = DllStructGetData($tInfo, "Item")
        ;CURRENT item checked
        ;place your code HERE
    Case $LVM_UNCHECKED
        $iLastItemChecked = -1
EndSwitch
Link to comment
Share on other sites

I'm sorry but as there is only one item checked, the last item checked is the item currently checked. OR I am missing something there?

Edit: The variable name is $iLastItemChecked, but it can be $iCurrentItemChecked until there is another one checked.

Edit2: To be more clear, it depends where you are using the variable.

-If you are using it in the WM_NOTIFY func before this line :

$iLastItemChecked = DllStructGetData($tInfo, "Item")

then it's the LAST item checked, which will be unchecked.

-If you are using it at/after this line, it's the CURRENT item checked.

So the var name should be $iCurrentItemChecked, maybe that's why it confused you.

Br, FireFox.

Thanks, I moved my code after this line, and it works perfectly now.

Link to comment
Share on other sites

Another question though, for anyone who may know the answer, how would I go abotu getting the row checked on the startup of the program? I tried once using a loop and checking each row to see if it was checked or not, but it didn't seem to work properly.

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