AlecSadler Posted July 23, 2016 Share Posted July 23, 2016 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 More sharing options...
InunoTaishou Posted July 23, 2016 Share Posted July 23, 2016 _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 More sharing options...
Moderators Melba23 Posted July 24, 2016 Moderators Share Posted July 24, 2016 AlecSadler, Or you can use the native commands like this: expandcollapse popup#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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now