Jump to content
Sign in to follow this  

please help with ListView selection

Recommended Posts


$gui_lv = GUICtrlCreateListView("", 10, 30, 940, 250)
_GUICtrlListView_AddColumn($gui_lv, "A",220)
_GUICtrlListView_AddColumn($gui_lv, "B",200)
_GUICtrlListView_AddColumn($gui_lv, "C",70)

for $i = 1 to $data[0]
$g_entry[$i] = GUICtrlCreateListViewItem($data[$i],$gui_lv)
GUICtrlCreateLabel("Choose:", 10, 10 )

$g_select_item = GUICtrlCreateButton("Select", 10, 290,100)
$g_cancel = GUICtrlCreateButton("Cancel", 120,290,100)


$msg = GUIGetMsg()
case $msg = $GUI_EVENT_CLOSE
case $msg = $g_cancel
case $msg = $g_select_item
MsgBox(0,"",GUICtrlRead($gui_lv) & @CRLF & GUICtrlGetState($gui_lv) ) ; always returns 4 and -1
until $msg = $GUI_EVENT_CLOSE


I would like to have the row (or row number) returned when the "Select" button is pressed. However the MsgBox always returns 4 and -1. What am I doing wrong?



Share this post

Link to post
Share on other sites

Change the msgbox line to this and see if it works.

MsgBox(0, "",  _GUICtrlListView_GetSelectedIndices($gui_lv) & @CRLF & GUICtrlGetState($gui_lv)) ; always returns 4 and -1

Not sure what you're trying to get with the GUICtrlGetState part so I haven't touched that.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post

Link to post
Share on other sites


It's a lot easier to get help if you post runnable code. The following code builds on BrewmanNH's suggestion, see comments in code.

; added the following to get the code to run 

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

local $data[5] = [4,1,2,3,4]
local $g_entry[ubound($data)]
local $gui010   =   guicreate('Test LV',510,350)

; end of code added to get the code to run 

$gui_lv = GUICtrlCreateListView("", 10, 30, 490, 250,$lvs_showselalways) ; will override single item selection
_GUICtrlListView_AddColumn($gui_lv, "A", 220)
_GUICtrlListView_AddColumn($gui_lv, "B", 200)
_GUICtrlListView_AddColumn($gui_lv, "C", 70)

For $i = 1 To $data[0]
    $g_entry[$i] = GUICtrlCreateListViewItem($data[$i], $gui_lv)
GUICtrlCreateLabel("Choose:", 10, 10)

$g_select_item = GUICtrlCreateButton("Select", 10, 290, 100)
$g_cancel = GUICtrlCreateButton("Cancel", 120, 290, 100)


    $msg = GUIGetMsg()
        Case $msg = $GUI_EVENT_CLOSE
        Case $msg = $g_cancel
        Case $msg = $g_select_item

            ; use the following loop if allowing for multiple selections ($lvs_singlesel ovveridden)

            local $aSEL = _GUICtrlListView_GetSelectedIndices($gui_lv,true)
            for $1 = 1 to  $aSEL[0]
                consolewrite('Item # ' & $aSEL[$1] & ' selected...value = ' & _GUICtrlListView_Getitemtextstring($gui_lv,$aSEL[$1]) & @lf)

            ; the following can be used for the current item selected, or, 1ST item selected in a list of items

            consolewrite(_GUICtrlListView_Getitemtextstring($gui_lv,-1) & @lf)
Until $msg = $GUI_EVENT_CLOSE



Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post

Link to post
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
Sign in to follow this