Jump to content

get listview selected row


Recommended Posts

Hello I am having some trouble figuring out how to get the selected row of a list view. I can see how to get selected items of other controls but I see no function to get the selected row of a list view. Please advise me on how to continue. I looked for other answers on how to do this and couldnt find one that answered this question.

Link to comment
Share on other sites

_GUICtrlListView_GetItem will return the information about the item at the specified index. Here's how you'd use it to find out which item is selected

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

Example()

Func Example()
    Local $iSelected = -1
    Local $frmExample = GUICreate("ListView Get Item", 400, 300)

    Local $lvExample = GUICtrlCreateListView("Items", 2, 2, 394, 268)
    Local $hLvExample = GUICtrlGetHandle($lvExample)
    Local $lblSelected = GUICtrlCreateLabel("Row Selected: None", 2, 275, 294)
    GUISetState(@SW_SHOW)

    For $i = 1 to 10
        GUICtrlCreateListViewItem("Row " & $i, $lvExample)
    Next

    Do
        For $i = 0 to _GUICtrlListView_GetItemCount($hLvExample)
            Local $aItemAttrib = _GUICtrlListView_GetItem($hLvExample, $i)
            ; Using bitand to see if the item has focus or is selected since the attribute at [0] can be a combination of values
            If (IsArray($aItemAttrib) and BitAND($aItemAttrib[0], 12)) Then
                ; Prevent the label from consntantly being updated, only updates when new item is selected
                If ($iSelected <> $i) Then
                    GUICtrlSetData($lblSelected, "Row Selected: " & $i + 1)
                    $iSelected = $i
                EndIf
                ExitLoop
            EndIf
        Next
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    GUIDelete($frmExample)
EndFunc   ;==>Example

 

Link to comment
Share on other sites

  • Moderators

AlecSadler,

Or you can use the native commands like this:

#include <GUIConstantsEx.au3>

Example()

Func Example()

    Local $sSelected = "", $cSelected, $cLastSel = 0

    Local $frmExample = GUICreate("ListView Get Item", 400, 300)

    Local $lvExample = GUICtrlCreateListView("Items", 2, 2, 394, 268)
    Local $lblSelected = GUICtrlCreateLabel("Item Selected: None", 2, 275, 294)

    GUISetState(@SW_SHOW)

    ; This is the ControlID just before the ListView items start - it allows us to calculate the row number
    $cStart = GUICtrlCreateDummy()

    For $i = 1 to 10
        GUICtrlCreateListViewItem("Item " & Chr($i + 64), $lvExample)
    Next

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch

        ; Get ControlID of selected row
        $cSelected = GUICtrlRead($lvExample)
        ; If a row selected and the selection has changed
        If $cSelected <> 0 And $cSelected <> $cLastSel Then
            ; Alter the label to show the row content - note requirement to strip the trailing delimiter from the return - and calculate the row number
            GUICtrlSetData($lblSelected, "Item Selected: " & StringTrimRight(GUICtrlRead($cSelected), 1) & " - which is in Row " & $cSelected - $cStart)
            ; Store the current selection
            $cLastSel = $cSelected
        EndIf

    WEnd

    GUIDelete($frmExample)
EndFunc   ;==>Example

Note that the "dummy start" row calculation trick only works if you create all the ListView items at the same time so that they have consecutive ControlIDs - if you add/delete items then you need rather more code.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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