Jump to content

Get selcected items in List View


amf
 Share

Recommended Posts

Hi all

I have a stange little problem... or actually two.

What I want to do is get the index of the selected item in a List View.

I wrote this code and when I try to get the index in function: Lst_ValuesClick()

and show it with: MsgBox(0, "$SelIndex: ", $SelIndex), the value of the index is shown

correct. When I use it in: MsgBox(0, "$SelItemTxt", _GUICtrlListView_GetItemText($Lst_Values, $SelIndex)),

the value is empty.

Another thing is that I want to have the selected value to the editbox by clicking on it in the List View,

but nothing happens when I click the values... only when I click on the Columnname.

Anyone have any ideas?

Here is the code...

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

$Frm_Main = GUICreate("Main", 202, 270, 193, 123)
GUISetOnEvent($GUI_EVENT_CLOSE, "Frm_MainClose")
$Lst_Values = GUICtrlCreateListView("Items", 16, 16, 169, 175)
GUICtrlSetOnEvent(-1, "Lst_ValuesClick")
$Inp_Value = GUICtrlCreateInput("", 16, 200, 169, 21)
$Btn_Add = GUICtrlCreateButton("Add to list", 16, 232, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "Btn_AddClick")
$Btn_Delete = GUICtrlCreateButton("Delete", 112, 232, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "Btn_DeleteClick")
GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func Btn_AddClick()
    _GUICtrlListView_AddItem($Lst_Values, GUICtrlRead($Inp_Value))
EndFunc

Func Btn_DeleteClick()
    _GUICtrlListView_DeleteItemsSelected($Lst_Values) ;deleting selected item
EndFunc

Func Frm_MainClose()
    Exit
EndFunc

Func Lst_ValuesClick()
    $SelIndex =  _GUICtrlListView_GetSelectedIndices($Lst_Values))
    MsgBox(0, "$SelIndex: ", $SelIndex)
    MsgBox(0, "$SelItemTxt", _GUICtrlListView_GetItemText($Lst_Values, $SelIndex))
EndFunc
Edited by amf
Link to comment
Share on other sites

  • Moderators

amf,

Answer 1: _GUICtrlListView_GetItemText is one of those UDF function that requires the handle of the control and not its ControlID.

Answer 2: Try using GUIRegisterMsg to react to clicks on the ListView. It is best to use double clicks, as you need single clicks to actually select the value. :)

Then you get something like this:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

Global $fDblClk = False

Opt("GUIOnEventMode", 1)

$Frm_Main = GUICreate("Main", 202, 270, 193, 123)
GUISetOnEvent($GUI_EVENT_CLOSE, "Frm_MainClose")
$Lst_Values = GUICtrlCreateListView("Items", 16, 16, 169, 175)
$Lst_Handle = GUICtrlGetHandle(-1)
$Inp_Value = GUICtrlCreateInput("", 16, 200, 169, 21)
$Btn_Add = GUICtrlCreateButton("Add to list", 16, 232, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "Btn_AddClick")
$Btn_Delete = GUICtrlCreateButton("Delete", 112, 232, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "Btn_DeleteClick")
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_ListView_DoubleClick")

While 1
    Sleep(10)

    If $fDblClk = True Then
        $fDblClk = False
        Lst_ValuesClick()
    EndIf

WEnd

Func Btn_AddClick()
    _GUICtrlListView_AddItem($Lst_Values, GUICtrlRead($Inp_Value))
EndFunc

Func Btn_DeleteClick()
    _GUICtrlListView_DeleteItemsSelected($Lst_Values) ;deleting selected item
EndFunc

Func Frm_MainClose()
    Exit
EndFunc

Func Lst_ValuesClick()
    $SelIndex =  _GUICtrlListView_GetSelectedIndices($Lst_Values)
    MsgBox(0, "$SelIndex: ", $SelIndex)
    MsgBox(0, "$SelItemTxt", _GUICtrlListView_GetItemText($Lst_Handle, $SelIndex))

EndFunc

; React to double clicks on recent ListView
Func WM_ListView_DoubleClick($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $wParam
    Local $tNMHDR = DllStructCreate("int;int;int", $lParam)
    If @error Then Return
    If DllStructGetData($tNMHDR, 1) = $Lst_Handle Then
        If DllStructGetData($tNMHDR, 3) = $NM_DBLCLK Then $fDblClk = True
    EndIf
    $tNMHDR = 0
    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_ListView_DoubleClick

I hope that helps. :(

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

  • Moderators

amf,

My pleasure. :(

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