Jump to content

Listview Selected item value


 Share

Recommended Posts

Hello,

I have something like that :

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
#include <Array.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
Global $fDblClk = False
Global $ar_Array
$Form1 = GUICreate("Services", 633, 447, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Sortie")
$List1 = GUICtrlCreateListView("", 8, 8, 617, 383)
_GUICtrlListView_AddColumn($List1, "Servcies Name", 200)
_GUICtrlListView_AddColumn($List1, "Status", 200)
_GUICtrlListView_AddColumn($List1, "Description", 200)
$Lst_Handle = GUICtrlGetHandle(-1)
$Button1 = GUICtrlCreateButton("Lister", 144, 408, 97, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button1, "Lister")  ; *****
$Button2 = GUICtrlCreateButton("Exit", 336, 408, 97, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button2, "Sortie")  ; *****
GUIRegisterMsg($WM_NOTIFY, "WM_ListView")
GUISetState(@SW_SHOW, $Form1)
Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($List1)]
#EndRegion ### END Koda GUI section ###
; Variables à déclarer impérativement avant la boucle d'attente.
Global $Form2, $Combo1, $Button3
While 1
    If $fDblClk = True Then
        $fDblClk = False
        Lst_ValuesClick()
    EndIf
    If GUICtrlRead($Combo1) <> "" Then
        GUICtrlSetState($Button3, $GUI_ENABLE)
    Endif
    Sleep(10)
WEnd
Func Lister()
    _GUICtrlListView_DeleteAllItems($List1)
    $strComputer = "127.0.0.1"
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
    $colProcessList = $objWMIService.ExecQuery("Select * from Win32_Service")
    For $objProcess In $colProcessList
        $services = $objProcess.Caption
        $status = $objProcess.State
        $Description = $objProcess.Description
        GUICtrlCreateListViewItem($services & "|" & $status & "|" & $Description, $List1)
    Next
EndFunc
Func Lst_ValuesClick()
    $SelIndexItem =  _GUICtrlListView_GetSelectedIndices($List1) +0
    $SelIndex = _GUICtrlListView_GetItem($List1, $SelIndexItem)
_ArrayDisplay($SelIndex)
    $Form2 = GUICreate("Action à réaliser", 333, 159, 327, 268)
    GUISetOnEvent($GUI_EVENT_CLOSE, "Sortie")  ; *****
    $Label1 = GUICtrlCreateLabel("Vous avez sélectionner le service : ", 16, 16, 171, 17)
    $Label2 = GUICtrlCreateLabel("", 16, 40, 308, 20)
    $Label3 = GUICtrlCreateLabel("Action : ", 16, 80, 43, 17)
    $Combo1 = GUICtrlCreateCombo("", 64, 80, 60, 25)
    GUICtrlSetData(-1, "Start|Stop|Restart")
    $Button3 = GUICtrlCreateButton("Lancer", 224, 80, 89, 25, $WS_GROUP)
    GUICtrlSetOnEvent($Button3, "Fonction_X1")  ; *****
    $Button4 = GUICtrlCreateButton("Fermer", 224, 110, 89, 25, $WS_GROUP)
    GUICtrlSetOnEvent($Button4, "Fonction_X2")  ; *****
    GUISetState()
    GUICtrlSetData($Label2, $SelIndex[3])
    GUICtrlSetState($Button3, $GUI_DISABLE)
EndFunc
Func WM_ListView($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
$hWndListView = $List1
If Not IsHWnd($List1) Then $hWndListView = GUICtrlGetHandle($List1)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $iCode
  Case $NM_DBLCLK
   $fDblClk = True
  Case $LVN_COLUMNCLICK
   $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
   _GUICtrlListView_SimpleSort($List1, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
Func Fonction_X1()  ; *****
    GUIDelete($Form2)  ; *****
    MsgBox(32, 'Message', 'Action Lancer')  ; *****
EndFunc  ; *****
Func Fonction_X2()  ; *****
    GUIDelete($Form2)  ; *****
    MsgBox(32, 'Message', 'Action fermer')  ; *****
EndFunc  ; *****
Func Sortie()  ; *****
    If @GUI_WinHandle = $Form1 Then  ; *****
        Exit
    Else
        GUIDelete(@GUI_WinHandle)
    EndIf
EndFunc

What i want to do, is to return (in an array) the value of each colum on the selected listview item after Dbclick on it.

No matter if the listvew have been reorder by colum, and no matter how many colum there is (in my case 3, but its an exemple, i'm working with a listview with 12 colum atm).

i have try :

ControlListView

_GUICtrlListView_GetSelectedIndices

_GUICtrlListView_GetItem

other thing i cant remeber.

Thx for your help ^^

Link to comment
Share on other sites

Use _GUICtrlListView_GetItemTextArray or _GUICtrlListView_GetItemTextString

and add Int() to returned index from _GUICtrlListView_GetSelectedIndices or use modified version _GUICtrlListView_GetSelectedIndex

Edit: forgot -1 on item count in _GUICtrlListView_GetSelectedIndex, removed unused var

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>

Opt("GUIOnEventMode", 1)

#region ### START Koda GUI section ### Form=
Global $fDblClk = False
Global $ar_Array

$Form1 = GUICreate("Services", 633, 447, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Sortie")
$List1 = GUICtrlCreateListView("", 8, 8, 617, 383)
_GUICtrlListView_AddColumn($List1, "Servcies Name", 200)
_GUICtrlListView_AddColumn($List1, "Status", 200)
_GUICtrlListView_AddColumn($List1, "Description", 200)
$Lst_Handle = GUICtrlGetHandle(-1)
$Button1 = GUICtrlCreateButton("Lister", 144, 408, 97, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button1, "Lister") ; *****
$Button2 = GUICtrlCreateButton("Exit", 336, 408, 97, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button2, "Sortie") ; *****
GUIRegisterMsg($WM_NOTIFY, "WM_ListView")
GUISetState(@SW_SHOW, $Form1)
Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($List1)]
#endregion ### END Koda GUI section ###

; Variables à déclarer impérativement avant la boucle d'attente.
Global $Form2, $Combo1, $Button3

While 1
If $fDblClk = True Then
  $fDblClk = False
  Lst_ValuesClick()
EndIf
If GUICtrlRead($Combo1) <> "" Then
  GUICtrlSetState($Button3, $GUI_ENABLE)
EndIf
Sleep(10)
WEnd

Func Lister()
_GUICtrlListView_DeleteAllItems($List1)
$strComputer = "127.0.0.1"
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootcimv2")
$colProcessList = $objWMIService.ExecQuery("Select * from Win32_Service")
For $objProcess In $colProcessList
  $services = $objProcess.Caption
  $status = $objProcess.State
  $Description = $objProcess.Description
  GUICtrlCreateListViewItem($services & "|" & $status & "|" & $Description, $List1)
Next
EndFunc   ;==>Lister

Func Lst_ValuesClick()
;as double clicking selects a single item you could convert returned String to Int or use this stripped down version: _GUICtrlListView_GetSelectedIndex
;$SelIndexItem = Int(_GUICtrlListView_GetSelectedIndices($List1)) ;returns delimited string of selected items or single item
$SelIndexItem = _GUICtrlListView_GetSelectedIndex($List1)
$SelIndex = _GUICtrlListView_GetItemTextArray($List1, $SelIndexItem)
;$SelIndex = _GUICtrlListView_GetItem($List1, $SelIndexItem)
_ArrayDisplay($SelIndex)
$Form2 = GUICreate("Action à réaliser", 333, 159, 327, 268)
GUISetOnEvent($GUI_EVENT_CLOSE, "Sortie") ; *****
$Label1 = GUICtrlCreateLabel("Vous avez sélectionner le service : ", 16, 16, 171, 17)
$Label2 = GUICtrlCreateLabel("", 16, 40, 308, 20)
$Label3 = GUICtrlCreateLabel("Action : ", 16, 80, 43, 17)
$Combo1 = GUICtrlCreateCombo("", 64, 80, 60, 25)
GUICtrlSetData(-1, "Start|Stop|Restart")
$Button3 = GUICtrlCreateButton("Lancer", 224, 80, 89, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button3, "Fonction_X1") ; *****
$Button4 = GUICtrlCreateButton("Fermer", 224, 110, 89, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button4, "Fonction_X2") ; *****
GUISetState()
GUICtrlSetData($Label2, $SelIndex[3])
GUICtrlSetState($Button3, $GUI_DISABLE)
EndFunc   ;==>Lst_ValuesClick

;get selected index (item doubleclicked on) (for single select listview)
Func _GUICtrlListView_GetSelectedIndex($hWnd)
Local $iRet, $iCount = GUICtrlSendMsg($hWnd, $LVM_GETITEMCOUNT, 0, 0)
For $iItem = 0 To $iCount -1
  $iRet = GUICtrlSendMsg($hWnd, $LVM_GETITEMSTATE, $iItem, $LVIS_SELECTED)
  If $iRet Then Return $iItem
Next
EndFunc   ;==>_GUICtrlListView_GetSelectedIndex

Func WM_ListView($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
$hWndListView = $List1
If Not IsHWnd($List1) Then $hWndListView = GUICtrlGetHandle($List1)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $iCode
  Case $NM_DBLCLK
   $fDblClk = True
  Case $LVN_COLUMNCLICK
   $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
   _GUICtrlListView_SimpleSort($List1, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_ListView

Func Fonction_X1() ; *****
GUIDelete($Form2) ; *****
MsgBox(32, 'Message', 'Action Lancer') ; *****
EndFunc   ;==>Fonction_X1
Func Fonction_X2() ; *****
GUIDelete($Form2) ; *****
MsgBox(32, 'Message', 'Action fermer') ; *****
EndFunc   ;==>Fonction_X2

Func Sortie() ; *****
If @GUI_WinHandle = $Form1 Then ; *****
  Exit
Else
  GUIDelete(@GUI_WinHandle)
EndIf
EndFunc   ;==>Sortie
Edited by rover

I see fascists...

Link to comment
Share on other sites

I use _GUICtrlListView_GetNextItem() to get selected item index:

; current selected (0 - first column)
    $col_value = _GUICtrlListView_GetItemText($ListView1, _GUICtrlListView_GetNextItem($ListView1), 0)
    If $col_value = -1 Then Return
Edited by Zedna
Link to comment
Share on other sites

Thanks ;)

Edit :

by the way, is there is a possibility to add something that disable the possibility to use the parent GUI while the child is open ?

in this exemple i mean :

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>
Opt("GUIOnEventMode", 1)
#region ### START Koda GUI section ### Form=
Global $fDblClk = False
Global $ar_Array
$Form1 = GUICreate("Services", 633, 447, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Sortie")
$List1 = GUICtrlCreateListView("", 8, 8, 617, 383)
_GUICtrlListView_AddColumn($List1, "Servcies Name", 200)
_GUICtrlListView_AddColumn($List1, "Status", 200)
_GUICtrlListView_AddColumn($List1, "Description", 200)
$Lst_Handle = GUICtrlGetHandle(-1)
$Button1 = GUICtrlCreateButton("Lister", 144, 408, 97, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button1, "Lister") ; *****
$Button2 = GUICtrlCreateButton("Exit", 336, 408, 97, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button2, "Sortie") ; *****
GUIRegisterMsg($WM_NOTIFY, "WM_ListView")
GUISetState(@SW_SHOW, $Form1)
Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($List1)]
#endregion ### END Koda GUI section ###
; Variables à déclarer impérativement avant la boucle d'attente.
Global $Form2, $Combo1, $Button3
While 1
If $fDblClk = True Then
  $fDblClk = False
  Lst_ValuesClick()
EndIf
If GUICtrlRead($Combo1) <> "" Then
  GUICtrlSetState($Button3, $GUI_ENABLE)
EndIf
Sleep(10)
WEnd
Func Lister()
_GUICtrlListView_DeleteAllItems($List1)
$strComputer = "127.0.0.1"
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootcimv2")
$colProcessList = $objWMIService.ExecQuery("Select * from Win32_Service")
For $objProcess In $colProcessList
  $services = $objProcess.Caption
  $status = $objProcess.State
  $Description = $objProcess.Description
  GUICtrlCreateListViewItem($services & "|" & $status & "|" & $Description, $List1)
Next
EndFunc   ;==>Lister
Func Lst_ValuesClick()
;as double clicking selects a single item you could convert returned String to Int or use this stripped down version: _GUICtrlListView_GetSelectedIndex
;$SelIndexItem = Int(_GUICtrlListView_GetSelectedIndices($List1)) ;returns delimited string of selected items or single item
$SelIndexItem = _GUICtrlListView_GetSelectedIndex($List1)
$SelIndex = _GUICtrlListView_GetItemTextArray($List1, $SelIndexItem)
;$SelIndex = _GUICtrlListView_GetItem($List1, $SelIndexItem)
_ArrayDisplay($SelIndex)
$Form2 = GUICreate("Action à réaliser", 333, 159, 327, 268)
GUISetOnEvent($GUI_EVENT_CLOSE, "Sortie") ; *****
$Label1 = GUICtrlCreateLabel("Vous avez sélectionner le service : ", 16, 16, 171, 17)
$Label2 = GUICtrlCreateLabel("", 16, 40, 308, 20)
$Label3 = GUICtrlCreateLabel("Action : ", 16, 80, 43, 17)
$Combo1 = GUICtrlCreateCombo("", 64, 80, 60, 25)
GUICtrlSetData(-1, "Start|Stop|Restart")
$Button3 = GUICtrlCreateButton("Lancer", 224, 80, 89, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button3, "Fonction_X1") ; *****
$Button4 = GUICtrlCreateButton("Fermer", 224, 110, 89, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button4, "Fonction_X2") ; *****
GUISetState()
GUICtrlSetData($Label2, $SelIndex[3])
GUICtrlSetState($Button3, $GUI_DISABLE)
EndFunc   ;==>Lst_ValuesClick
;get selected index (item doubleclicked on) (for single select listview)
Func _GUICtrlListView_GetSelectedIndex($hWnd)
Local $iRet, $iCount = GUICtrlSendMsg($hWnd, $LVM_GETITEMCOUNT, 0, 0)
For $iItem = 0 To $iCount -1
  $iRet = GUICtrlSendMsg($hWnd, $LVM_GETITEMSTATE, $iItem, $LVIS_SELECTED)
  If $iRet Then Return $iItem
Next
EndFunc   ;==>_GUICtrlListView_GetSelectedIndex
Func WM_ListView($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
$hWndListView = $List1
If Not IsHWnd($List1) Then $hWndListView = GUICtrlGetHandle($List1)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $iCode
  Case $NM_DBLCLK
   $fDblClk = True
  Case $LVN_COLUMNCLICK
   $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
   _GUICtrlListView_SimpleSort($List1, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_ListView
Func Fonction_X1() ; *****
GUIDelete($Form2) ; *****
MsgBox(32, 'Message', 'Action Lancer') ; *****
EndFunc   ;==>Fonction_X1
Func Fonction_X2() ; *****
GUIDelete($Form2) ; *****
MsgBox(32, 'Message', 'Action fermer') ; *****
EndFunc   ;==>Fonction_X2
Func Sortie() ; *****
If @GUI_WinHandle = $Form1 Then ; *****
  Exit
Else
  GUIDelete(@GUI_WinHandle)
EndIf
EndFunc

^^

Edited by JeromeB
Link to comment
Share on other sites

Disable main form when child is run

GUISetState(@SW_DISABLE, $Form1)

Guinness coded a dimmed effect on a gui that you could also use along with disabling when the main gui loses focus

Good catch Zedna.

I looked at my last (shelved) project with a listview and that is what I was using...

I should check my own code first, instead of coding off-the-cuff.

Edited by rover

I see fascists...

Link to comment
Share on other sites

Thank.

A last question if you permit me.

I use to create my listview : GUICtrlCreateListView

i use to add collum : _GUICtrlListView_InsertColumn

the things is for add value in list view (which got 12 collum and about 30 lines)

i use atm an array by that :

For $i = 1 to UBound($aData) - 1
  $string = ""
  For $j = 0 to 12
   $string = $string & $aData[$i][$j] & "|"
  Next
  GUICtrlCreateListViewItem($string, $ListView)
Next

Which is pretty slow,

instead, i have seen this function :

_GUICtrlListView_AddArray($ListView,$aData)

Which work pretty nice and fast to add all the array (what i want)

But the thing is, when i try to delete all the item in the list (to re-list)

i use this : _GUICtrlListView_DeleteAllItems($ListView)

Which is working with the doble loop for, but not working with the _GUICtrlListView_AddArray

I know there is some trouble using those two type, but i cant create my listview by using the _guictrlcreatelistview things...

Any idea how to delete the all listview when the value have been added with _GUICtrlListView_AddArray ?

Link to comment
Share on other sites

If you want to use _GUICtrlListView_DeleteAllItems(), and the items in the listview were created using the UDF functions, you HAVE to send this function the HANDLE of the listview, and not the control ID which is what the function GUICtrlCreateListview returns.So you'd need to do it this way -> _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($ListView)) instead.

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

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