Jump to content

Get listview selected item


lucamad
 Share

Recommended Posts

Hi!

I'm a AutoIt newbie. I add some items in my listview control, but i can't get the selected item info.

I need to use icons in my listview, so i've used the _GUICtrlListView_AddItem function (the GUICtrlCreateListViewItem doesn't support icons...):

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

GUICreate("Lokyweb Uploader", -1, -1, -1, -1, BitOr($WS_SIZEBOX, $WS_SYSMENU, $WS_MAXIMIZEBOX, $WS_MINIMIZEBOX), $WS_EX_ACCEPTFILES);x il drag & drop

$listview = GUICtrlCreateListView("List", 2, 40, 394, 268, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT, $LVS_SINGLESEL))
_GUICtrlListView_AddItem($listview, "test", 1)

$button = GUICtrlCreateButton("Selected item", 10, 325)
GUISetState()

While (1)
    $msg = GUIGetMsg()
    
    if $msg = $button Then
        msgbox (0, "Selected item", GUICtrlRead(GUICtrlRead($listview)) )
    EndIf
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

The msgbox always return a 0.

There is a way to obtain a selected value in listview with icons?

Thanks!!

Link to comment
Share on other sites

  • Moderators

lucamad,

Welcome to the AutoIt forum. :(

If you add the items to the ListView with the UDF, you need to carry on using the UDF on the items created:

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

GUICreate("Lokyweb Uploader", -1, -1, -1, -1, BitOr($WS_SIZEBOX, $WS_SYSMENU, $WS_MAXIMIZEBOX, $WS_MINIMIZEBOX), $WS_EX_ACCEPTFILES);x il drag & drop

$listview = GUICtrlCreateListView("List", 2, 40, 394, 268, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT, $LVS_SINGLESEL))
_GUICtrlListView_AddItem($listview, "test1", 1)
_GUICtrlListView_AddItem($listview, "test2", 2)

$button = GUICtrlCreateButton("Selected item", 10, 325)
GUISetState()

While (1)
    $msg = GUIGetMsg()

    if $msg = $button Then
        $iIndex = _GUICtrlListView_GetSelectedIndices($listview)
        msgbox (0, "Selected item", $iIndex)
    EndIf
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

This will return the 0-based index of the selected item.

Be very careful mixing the buit-in commands and those from the UDF (regardless of which control type) as it can often end in tears. :)

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

Melba23

Many thanks! In fact I've spent a lot of time for this problem... Your corrections work fine. I don't know why the behavior is different using UDF functions (a listview is already a listview ..or not? :( ). This script language is very interesting and adaptable, I like it very much. THX Melba Luca

Link to comment
Share on other sites

  • Moderators

lucamad,

a listview is already a listview ..or not?

"Not" - or at least "Not quite"! :)

When you create a ListViewItem with the built-in command GUICtrlCreateListViewItem, AutoIt returns an internal ControlID and you can then use (most of) the other builtin GUICtrl* commands on the item using the ControlID to identify it.

When you use the UDF's _GUICtrlListView_AddItem command, it returns the unique Windows handle of the control, which means that (most of) the built-in Autoit commands will not work as there is no ControlID to identify the control to AutoIt's internal coding.

That is a bit simplified, but shows in brief why you should not, in general, mix the built-in and UDF commands for a control. Go for one or the other - although there are plenty of examples where coders have done it and it works! :)

I hope that makes it a bit clearer. :(

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