Jump to content

Problem getting selected item from listview...


Recommended Posts

I was trying to figure how to get the text of the selected item of a listview.

So I came up with this, thinking it'd work:

_GUICtrlListView_GetItemText($hListView, _GUICtrlListView_GetSelectedIndices($hListView))

But it doesn't. What have I done wrong?

Try Pacfox, my Firefox theme.Try Power Eject, my windows gadget that allows you to eject most drives.Using AutoIt 3.3.4.0, Windows 7 Premium, Intel Core 2 Quad CPU @ 2.66ghz, 4gb RAM, Nvidia GeForce 9500GT Graphics Card & Samsung 22" Monitor.
Link to comment
Share on other sites

  • Moderators

Valdersapien,

Have you looked at how the function returns the values? :D

If you set $fArray to True:

Array - With the following format

[0] - Number of Items in array (n)

[1] - First item index

[2] - Second item index

[n] - Last item index

If you set $fArray to False:

String - With the following format

"0|1|2|n"

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

Oh! So GUICtrlListView_GetSelectedIndices() (with $fArray set to false) returns a string, not a number. I hadn't noticed this since I was using my listview with $LVS_SINGLECEL(or whatever it is)...

So instead of creating reading an array (another variable!), I just converted the string to an integer:

_GUICtrlListView_GetItemText($hListView, Int(_GUICtrlListView_GetSelectedIndices($hListView)))

And it works perfect now.

Thanks for pointing out that fact, I probably would have never noticed it!

EDIT: Now how do I tell when an item is clicked on? GUIGetMsg() doesn't seem to work...

Edited by Vadersapien
Try Pacfox, my Firefox theme.Try Power Eject, my windows gadget that allows you to eject most drives.Using AutoIt 3.3.4.0, Windows 7 Premium, Intel Core 2 Quad CPU @ 2.66ghz, 4gb RAM, Nvidia GeForce 9500GT Graphics Card & Samsung 22" Monitor.
Link to comment
Share on other sites

And this way too:

#include <GuiListView.au3>
;some code here and etc.
    $si=_GUICtrlListView_GetSelectedIndices($List1)
    local $AA = _GUICtrlListView_GetItemText(GUICtrlGetHandle($List1),$si,0) ; is fisrt column data of cource selected. So if you retrive data only from second row replace it with 1
[size="5"] [/size]
Link to comment
Share on other sites

Alright...managed to simplify that WM_NOTIFY function mentioned in the help file:

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
;~  Local $tBuffer
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                            ConsoleWrite('Clicked')

            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

It works perfect, but can it be simplified any further?

Edited by Vadersapien
Try Pacfox, my Firefox theme.Try Power Eject, my windows gadget that allows you to eject most drives.Using AutoIt 3.3.4.0, Windows 7 Premium, Intel Core 2 Quad CPU @ 2.66ghz, 4gb RAM, Nvidia GeForce 9500GT Graphics Card & Samsung 22" Monitor.
Link to comment
Share on other sites

Not really. You could replace some of the variables to make the code shorter, but not simpler.

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndListView

    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

    Switch HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
        Case $hWndListView
            Switch DllStructGetData($tNMHDR, "Code")
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    ConsoleWrite('Clicked')
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Edited by dantay9
Link to comment
Share on other sites

One simple way, if you only need single click

; Listview get selected row(s) - gw

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Opt("MouseCoordMode", 2)
Global $iLvLeft = 10, $iLvTop = 10, $iLvWidth = 280, $iLvHeight = 180
Global $iWidth = 300, $iHeight = 230
$hGUI = GUICreate("Test", $iWidth, $iHeight)

$idListView = GUICtrlCreateListView("Items|SubItems", $iLvLeft, $iLvTop, $iLvWidth, $iLvHeight, $LVS_REPORT, $WS_EX_CLIENTEDGE)
$hListview = GUICtrlGetHandle($idListView)

_GUICtrlListView_SetExtendedListViewStyle($hListview, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

For $i = 0 To 10
    _GUICtrlListView_AddItem($hListview, "Item" & $i)
    _GUICtrlListView_AddSubItem($hListview, $i, "SubItem" & $i, 1)
Next

$idSelected = GUICtrlCreateButton("Get Selected", 8, $iHeight - 30, $iWidth - 16, 20)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_PRIMARYDOWN
            If MouseGetPos(1) > ($iLvTop + 20) And MouseGetPos(1) < ($iLvHeight + $iLvTop) Then GetSelected()
        Case $idSelected
            GetSelected()
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func GetSelected()
    ConsoleWrite(@LF)
    For $i = 0 To GUICtrlSendMsg($idListView, $LVM_GETITEMCOUNT, 0, 0)
        If GUICtrlSendMsg($idListView, $LVM_GETITEMSTATE, $i, 0x2) Then _
            ConsoleWrite("Selected Row: " & $i & " Text: " & _GUICtrlListView_GetItemText($hListview, $i) & " " & _GUICtrlListView_GetItemText($hListview, $i, 1) & @LF)
    Next
EndFunc
Otherwise handling wm_notify would be prefered.

Edited by picaxe
Link to comment
Share on other sites

@Picaxe - Thanks for the suggestion. I like your method, it's a fair bit simpler to work with..I can't really decide between the two, especially when it comes to having two listviews(what then?)

Try Pacfox, my Firefox theme.Try Power Eject, my windows gadget that allows you to eject most drives.Using AutoIt 3.3.4.0, Windows 7 Premium, Intel Core 2 Quad CPU @ 2.66ghz, 4gb RAM, Nvidia GeForce 9500GT Graphics Card & Samsung 22" Monitor.
Link to comment
Share on other sites

@Picaxe - Thanks for the suggestion. I like your method, it's a fair bit simpler to work with..I can't really decide between the two, especially when it comes to having two listviews(what then?)

More than one listview
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Opt("MouseCoordMode", 2)
Global $iLvLeft1 = 10, $iLvTop1 = 10, $iLvWidth1 = 280, $iLvHeight1 = 180
Global $iLvLeft2 = 300, $iLvTop2 = 10, $iLvWidth2 = 280, $iLvHeight2 = 180
Global $iWidth = 590, $iHeight = 230
$hGUI = GUICreate("Test", $iWidth, $iHeight)

$idListView1 = GUICtrlCreateListView("Items|SubItems", $iLvLeft1, $iLvTop1, $iLvWidth1, $iLvHeight1, $LVS_REPORT, $WS_EX_CLIENTEDGE)
$hListview1 = GUICtrlGetHandle($idListView1)
_GUICtrlListView_SetExtendedListViewStyle($hListview1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

$idListView2 = GUICtrlCreateListView("Items|SubItems", $iLvLeft2, $iLvTop2, $iLvWidth2, $iLvHeight2, $LVS_REPORT, $WS_EX_CLIENTEDGE)
$hListview2 = GUICtrlGetHandle($idListView2)
_GUICtrlListView_SetExtendedListViewStyle($hListview2, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

GUISetState()

For $i = 0 To 10
    _GUICtrlListView_AddItem($hListview1, "Item" & $i)
    _GUICtrlListView_AddSubItem($hListview1, $i, "SubItem" & $i, 1)
Next
For $i = 0 To 10
    _GUICtrlListView_AddItem($hListview2, "Item" & $i)
    _GUICtrlListView_AddSubItem($hListview2, $i, "SubItem" & $i, 1)
Next

$idSelected1 = GUICtrlCreateButton("Get Selected 1", $iLvLeft1, $iHeight - 30, $iLvWidth1, 20)
$idSelected2 = GUICtrlCreateButton("Get Selected 2", $iLvLeft2, $iHeight - 30, $iLvWidth2, 20)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_PRIMARYDOWN
            $aPos = MouseGetPos()
            If $aPos[1] > ($iLvTop1 + 20) And $aPos[1] < ($iLvHeight1 + $iLvTop1) Then
                If $aPos[0] > $iLvLeft1 And $aPos[0] < ($iLvWidth1 + $iLvLeft1) Then GetSelected($idListView1)
            EndIf
            If $aPos[1] > ($iLvTop2 + 20) And $aPos[1] < ($iLvHeight2 + $iLvTop2) Then
                If $aPos[0] > $iLvLeft2 And $aPos[0] < ($iLvWidth2 + $iLvLeft2) Then GetSelected($idListView2)
            EndIf
        Case $idSelected1
            GetSelected($idListView1)
        Case $idSelected2
            GetSelected($idListView2)
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func GetSelected($idLv)
    Switch $idLv
        Case $idListView1
            $sLv = 1
            $hLv = $hListview1
        Case $idListView2
            $sLv = 2
            $hLv = $hListview2
    EndSwitch
    For $i = 0 To GUICtrlSendMsg($idLv, $LVM_GETITEMCOUNT, 0, 0)
        If GUICtrlSendMsg($idLv, $LVM_GETITEMSTATE, $i, 0x2) Then _
                ConsoleWrite("Listview " & $sLv & " Selected Row: " & $i & " Text: " & _GUICtrlListView_GetItemText($hLv, $i) & " " & _GUICtrlListView_GetItemText($hLv, $i, 1) & @LF)
    Next
    _GUICtrlListView_SetItemSelected($hLv, -1, False)
EndFunc ;==>GetSelected
Edited by picaxe
Link to comment
Share on other sites

I made a small function to test if an item is selected. However it doesn't always work...

Func _GUICtrlListView_ItemSelected($hGUI, $hListview)
    $ListviewPos = ControlGetPos($hGUI, '', $hListview)
    $CursorInfo = GUIGetCursorInfo($hGUI)

    If GUIGetMsg() = $GUI_EVENT_PRIMARYDOWN And $CursorInfo[0] > $ListviewPos[0] And $CursorInfo[0] < ($ListviewPos[0] + $ListviewPos[2]) And _
    $CursorInfo[1] > $ListviewPos[1] And $CursorInfo[1] < ($ListviewPos[1] + $ListviewPos[3]) And _GUICtrlListView_GetSelectedIndices($hListview) <> ''  Then
        Return True
    Else
        Return False
    EndIf
EndFunc

I have a feeling it has to do with the GUIGetMsg() = $GUI_EVENT_PRIMARYDOWN bit...

I could have used $CursorInfo[2] to see if the mouse button was down but it wouldn't work...it just returned false all the time(anyone know why?)...

Try Pacfox, my Firefox theme.Try Power Eject, my windows gadget that allows you to eject most drives.Using AutoIt 3.3.4.0, Windows 7 Premium, Intel Core 2 Quad CPU @ 2.66ghz, 4gb RAM, Nvidia GeForce 9500GT Graphics Card & Samsung 22" Monitor.
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...