Sign in to follow this  
Followers 0
z0iid

Read multiple selection from GuiCtrlCreateListView

3 posts in this topic

#1 ·  Posted (edited)

I'm trying to figure out how to return either an array, or delimited string of all selected items in a ListView. I am populating the ListViewItems via text file. (You can create the txt files in the script dir with just a few lines to get the idea). When ever I try to return an array of all selected items, it only returns the top most item selected. I have tried:

GuiCtrlRead

_GuiCtrlListView_GetSelectedIndices

_GuiCtrlListView_GetItemTextArray

_GuiCtrlListView_GetItemText

_GuiCtrlListView_GetItemTextString

_GuiCtrlListView_GetNextItem

And variations building on those, and only the Indices function actually returns more than one item.

What am I doing wrong???

Thanks for any input.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <WinAPI.au3>
#Include <GuiListView.au3>
#include <GuiListBox.au3>


AutoItSetOption("ExpandEnvStrings", 1)

Dim $listofall, $listofall_array, $listofMD, $listofMD_array, $listofPODS, $listofPODS_array, $active_list_array, $stuff, $current_item

$listofall = @ScriptDir & "\all.txt"
$listofMD = @ScriptDir & "\md.txt"
$listofPODS = @ScriptDir & "\pods.txt"

Opt("GUIOnEventMode", 1)


#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("BIG.RED.BUTTON", 687, 442, -1, -1,-1)
GUISetBkColor(0x7A7A7A)
$ButtonOk = GUICtrlCreateButton("BIG.RED.BUTTON", 28, 290, 627, 137, $BS_DEFPUSHBUTTON)
GUICtrlSetFont(-1, 60, 400, 0, "Agency FB")
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlSetOnEvent(-1, "ButtonOkClick")
GuiCtrlSetState(-1,$GUI_ONTOP)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")

$ALLPCs = GUICtrlCreateListView("ALL PC'S", 25, 41, 154, 160, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 133)


_FileReadToArray($listofall, $listofall_array)

$Port_Array_Limit = UBound($listofall_array) - 1
For $i = 1 To $Port_Array_Limit
    $populate_all_box = String($listofall_array[$i])
    GUICtrlCreateListViewItem($populate_all_box, $ALLPCs)
Next

$Radio1 = GUICtrlCreateRadio("Local Admin", 32, 256, 213, 17)
GUICtrlSetFont(-1, 16, 400, 0, "Vrinda")
GUICtrlSetOnEvent(-1, "RadioSelect_la")
$Radio2 = GUICtrlCreateRadio("SWC Shell", 530, 256, 123, 17, BitOR($GUI_SS_DEFAULT_RADIO,$BS_RIGHTBUTTON))
GUICtrlSetFont(-1, 16, 400, 0, "Vrinda")
GUICtrlSetOnEvent(-1, "RadioSelect_ss")

$MDWorkstations = GUICtrlCreateListView("MD Workstations", 270, 41, 154, 160, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 133)

_FileReadToArray($listofMD, $listofMD_array)

$Port_Array_Limit = UBound($listofMD_array) - 1
For $i = 1 To $Port_Array_Limit
    $populate_MD_box = String($listofMD_array[$i])
    GUICtrlCreateListViewItem($populate_MD_box, $MDWorkstations)
Next

$PODS = GUICtrlCreateListView("PODS", 505, 41, 154, 160, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 133)

_FileReadToArray($listofPODS, $listofPODS_array)

$Port_Array_Limit = UBound($listofPODS_array) - 1
For $i = 1 To $Port_Array_Limit
    $populate_PODS_box = String($listofPODS_array[$i])
    GUICtrlCreateListViewItem($populate_PODS_box, $PODS)
Next

$Checkbox1 = GUICtrlCreateCheckbox("Select ALL", 32, 216, 97, 17)
$Checkbox2 = GUICtrlCreateCheckbox("Select ALL", 276, 210, 97, 17)
$Checkbox3 = GUICtrlCreateCheckbox("Select ALL", 511, 211, 97, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

    While 1
        $msg = GUIGetMsg()
        sleep(500)

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd


Func ButtonOkClick()

    If $status = "Local Admin" Then
        $schedtask_xml = @ScriptDir & "\localreboot.xml"
    ElseIf $status = "SWC Shell" Then
        $schedtask_xml = @ScriptDir & "\swcshellreboot.xml"
    EndIf
    $active_list = @ScriptDir & "\templist.txt"
    ;$data = GUICtrlRead(GUICtrlRead($ALLPCs, 1))
    ;$data = _GUICtrlListView_GetSelectedIndices($ALLPCs, False)
    $data = _GUICtrlListView_GetItemTextArray($ALLPCs, -1)
    _ArrayDisplay($data)
    ;MsgBox(0, "", $data)


    

EndFunc

Func Form2Close()
    Exit
EndFunc


Func RadioSelect_la()
    Global $status = GUICtrlRead($Radio1, 1)
EndFunc

Func RadioSelect_ss()
    Global $status = GUICtrlRead($Radio2, 1)
EndFunc

all.txt

Edited by z0iid

Share this post


Link to post
Share on other sites



Hi,

Get the selected Indices of the ListView then loop through the indices to get the text. eg:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <WinAPI.au3>
#Include <GuiListView.au3>
#include <GuiListBox.au3>


AutoItSetOption("ExpandEnvStrings", 1)

Dim $listofall, $listofall_array, $listofMD, $listofMD_array, $listofPODS, $listofPODS_array, $active_list_array, $stuff, $current_item

$listofall = @ScriptDir & "\all.txt"
$listofMD = @ScriptDir & "\md.txt"
$listofPODS = @ScriptDir & "\pods.txt"

Opt("GUIOnEventMode", 1)


#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("BIG.RED.BUTTON", 687, 442, -1, -1,-1)
GUISetBkColor(0x7A7A7A)
$ButtonOk = GUICtrlCreateButton("BIG.RED.BUTTON", 28, 290, 627, 137, $BS_DEFPUSHBUTTON)
GUICtrlSetFont(-1, 60, 400, 0, "Agency FB")
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlSetOnEvent(-1, "ButtonOkClick")
GuiCtrlSetState(-1,$GUI_ONTOP)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")

$ALLPCs = GUICtrlCreateListView("ALL PC'S", 25, 41, 154, 160, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 133)


_FileReadToArray($listofall, $listofall_array)

$Port_Array_Limit = UBound($listofall_array) - 1
For $i = 1 To $Port_Array_Limit
    $populate_all_box = String($listofall_array[$i])
    GUICtrlCreateListViewItem($populate_all_box, $ALLPCs)
Next

$Radio1 = GUICtrlCreateRadio("Local Admin", 32, 256, 213, 17)
GUICtrlSetFont(-1, 16, 400, 0, "Vrinda")
GUICtrlSetOnEvent(-1, "RadioSelect_la")
$Radio2 = GUICtrlCreateRadio("SWC Shell", 530, 256, 123, 17, BitOR($GUI_SS_DEFAULT_RADIO,$BS_RIGHTBUTTON))
GUICtrlSetFont(-1, 16, 400, 0, "Vrinda")
GUICtrlSetOnEvent(-1, "RadioSelect_ss")

$MDWorkstations = GUICtrlCreateListView("MD Workstations", 270, 41, 154, 160, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 133)

_FileReadToArray($listofMD, $listofMD_array)

$Port_Array_Limit = UBound($listofMD_array) - 1
For $i = 1 To $Port_Array_Limit
    $populate_MD_box = String($listofMD_array[$i])
    GUICtrlCreateListViewItem($populate_MD_box, $MDWorkstations)
Next

$PODS = GUICtrlCreateListView("PODS", 505, 41, 154, 160, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 133)

_FileReadToArray($listofPODS, $listofPODS_array)

$Port_Array_Limit = UBound($listofPODS_array) - 1
For $i = 1 To $Port_Array_Limit
    $populate_PODS_box = String($listofPODS_array[$i])
    GUICtrlCreateListViewItem($populate_PODS_box, $PODS)
Next

$Checkbox1 = GUICtrlCreateCheckbox("Select ALL", 32, 216, 97, 17)
$Checkbox2 = GUICtrlCreateCheckbox("Select ALL", 276, 210, 97, 17)
$Checkbox3 = GUICtrlCreateCheckbox("Select ALL", 511, 211, 97, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
;~         $msg = GUIGetMsg() ; Will not work with Opt("GUIOnEventMode", 1)
    Sleep(100)

;~         If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd


Func ButtonOkClick()
    Local $aSelcted, $aDdata
    $aSelcted = _GUICtrlListView_GetSelectedIndices($ALLPCs, True)
    If $aSelcted[0] > 0 Then
        Dim $aDdata[$aSelcted[0] + 1]
        $aDdata[0] = $aSelcted[0]
        For $i = 1 To $aSelcted[0]
            $aDdata[$i] = _GUICtrlListView_GetItemTextString($ALLPCs, $aSelcted[$i])
        Next
    EndIf
    If IsArray($aDdata) Then _ArrayDisplay($aDdata)
EndFunc

Func Form2Close()
    Exit
EndFunc


Func RadioSelect_la()
    Global $status = GUICtrlRead($Radio1, 1)
EndFunc

Func RadioSelect_ss()
    Global $status = GUICtrlRead($Radio2, 1)
EndFunc

Cheers

Share this post


Link to post
Share on other sites

Thanks - works great. I had tried to loop through the indices, but I wasn't calling it correctly. Thanks for the tip regarding guioneventmode - it was causing 25%cpu usage without the half second sleep.

Hi,

Get the selected Indices of the ListView then loop through the indices to get the text. eg:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <WinAPI.au3>
#Include <GuiListView.au3>
#include <GuiListBox.au3>


AutoItSetOption("ExpandEnvStrings", 1)

Dim $listofall, $listofall_array, $listofMD, $listofMD_array, $listofPODS, $listofPODS_array, $active_list_array, $stuff, $current_item

$listofall = @ScriptDir & "\all.txt"
$listofMD = @ScriptDir & "\md.txt"
$listofPODS = @ScriptDir & "\pods.txt"

Opt("GUIOnEventMode", 1)


#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("BIG.RED.BUTTON", 687, 442, -1, -1,-1)
GUISetBkColor(0x7A7A7A)
$ButtonOk = GUICtrlCreateButton("BIG.RED.BUTTON", 28, 290, 627, 137, $BS_DEFPUSHBUTTON)
GUICtrlSetFont(-1, 60, 400, 0, "Agency FB")
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlSetOnEvent(-1, "ButtonOkClick")
GuiCtrlSetState(-1,$GUI_ONTOP)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")

$ALLPCs = GUICtrlCreateListView("ALL PC'S", 25, 41, 154, 160, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 133)


_FileReadToArray($listofall, $listofall_array)

$Port_Array_Limit = UBound($listofall_array) - 1
For $i = 1 To $Port_Array_Limit
    $populate_all_box = String($listofall_array[$i])
    GUICtrlCreateListViewItem($populate_all_box, $ALLPCs)
Next

$Radio1 = GUICtrlCreateRadio("Local Admin", 32, 256, 213, 17)
GUICtrlSetFont(-1, 16, 400, 0, "Vrinda")
GUICtrlSetOnEvent(-1, "RadioSelect_la")
$Radio2 = GUICtrlCreateRadio("SWC Shell", 530, 256, 123, 17, BitOR($GUI_SS_DEFAULT_RADIO,$BS_RIGHTBUTTON))
GUICtrlSetFont(-1, 16, 400, 0, "Vrinda")
GUICtrlSetOnEvent(-1, "RadioSelect_ss")

$MDWorkstations = GUICtrlCreateListView("MD Workstations", 270, 41, 154, 160, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 133)

_FileReadToArray($listofMD, $listofMD_array)

$Port_Array_Limit = UBound($listofMD_array) - 1
For $i = 1 To $Port_Array_Limit
    $populate_MD_box = String($listofMD_array[$i])
    GUICtrlCreateListViewItem($populate_MD_box, $MDWorkstations)
Next

$PODS = GUICtrlCreateListView("PODS", 505, 41, 154, 160, BitOR($LVS_REPORT,$LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 133)

_FileReadToArray($listofPODS, $listofPODS_array)

$Port_Array_Limit = UBound($listofPODS_array) - 1
For $i = 1 To $Port_Array_Limit
    $populate_PODS_box = String($listofPODS_array[$i])
    GUICtrlCreateListViewItem($populate_PODS_box, $PODS)
Next

$Checkbox1 = GUICtrlCreateCheckbox("Select ALL", 32, 216, 97, 17)
$Checkbox2 = GUICtrlCreateCheckbox("Select ALL", 276, 210, 97, 17)
$Checkbox3 = GUICtrlCreateCheckbox("Select ALL", 511, 211, 97, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
;~         $msg = GUIGetMsg() ; Will not work with Opt("GUIOnEventMode", 1)
    Sleep(100)

;~         If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd


Func ButtonOkClick()
    Local $aSelcted, $aDdata
    $aSelcted = _GUICtrlListView_GetSelectedIndices($ALLPCs, True)
    If $aSelcted[0] > 0 Then
        Dim $aDdata[$aSelcted[0] + 1]
        $aDdata[0] = $aSelcted[0]
        For $i = 1 To $aSelcted[0]
            $aDdata[$i] = _GUICtrlListView_GetItemTextString($ALLPCs, $aSelcted[$i])
        Next
    EndIf
    If IsArray($aDdata) Then _ArrayDisplay($aDdata)
EndFunc

Func Form2Close()
    Exit
EndFunc


Func RadioSelect_la()
    Global $status = GUICtrlRead($Radio1, 1)
EndFunc

Func RadioSelect_ss()
    Global $status = GUICtrlRead($Radio2, 1)
EndFunc

Cheers

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  
Followers 0