Jump to content

How to add OnClick action for ListView item?


au3scr
 Share

Recommended Posts

How i can add onclick action for List View Item so when I click on listview item, I will get message box says: "You clicked $ItemName_I_Clicked_on" for example

I click on "Untitled - Notepad" from list and then script show msg box you clicked on "Untitled - Notepad" .

Posted Image

#include <guiconstants.au3>
#include <File.au3>
#include <String.au3>
#include <GuiEdit.au3>
#include <Process.au3>
#include <GUIListView.au3>
#include <GuiConstantsEx.au3>

$Debug_LV = False ; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work


GUICreate("Window mannager", 320, 400)
$List1 = GUICtrlCreateListView ( "windows", 3,3,314,180)
_GUICtrlListView_SetColumnWidth($List1,0,310);300 is the width of the 0th column. Change it to whatever you want
$var = WinList()
For $i = 1 to $var[0][0]
  ; Only display visble windows that have a title
  If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then
    GUICtrlCreateListViewItem($var[$i][0] , $List1)
  EndIf
Next
$selected = GUICtrlCreateInput("",3,183,314,20)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $list1
;~          MsgBox (0,0,"string from clicked label")
;### Tidy Error -> "endswitch" is closing previous "case"
        EndSwitch
;### Tidy Error -> "wend" is closing previous "switch"
    WEnd






Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then 
    Return 1
  Else
    Return 0
  EndIf

EndFunc
Link to comment
Share on other sites

I didnt find help there,Is is possible to do onclick action for list view item? Help file dont talk about it.

Open the helpfile from the startmenu, SciTE or from the AutoIt folder (AutoIt.chm or AutoIt3Help.exe)
Link to comment
Share on other sites

Open the helpfile from the startmenu, SciTE or from the AutoIt folder (AutoIt.chm or AutoIt3Help.exe)

there is lots of stuff but i cant find how to read clicked item name.

I also have script that worked (in past) but I cant use it with my current autoit version, I attatched it here.

Script I attatched has function made be some1 in this forum , but its too hard for me to understand.

taskbar2.au3

Link to comment
Share on other sites

  • 4 weeks later...

Here's what worked for me...

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
GUICreate("Sample ListView", 450, 350,-1, -1, $WS_OVERLAPPEDWINDOW)

$listview = GUICtrlCreateListView("Column1           |Column2             |Column3", 40, 40, 360, 225, BitOr($LVS_REPORT, $LVS_SHOWSELALWAYS), $LVS_EX_FULLROWSELECT)
$item1 = GUICtrlCreateListViewItem("A|B|C",$listview)
$item2 = GUICtrlCreateListViewItem("D|E|F",$listview)
$item3 = GUICtrlCreateListViewItem("G|H|I",$listview)
$item4 = GUICtrlCreateListViewItem("J|K|L",$listview)

GUISetState()

While 1
    Sleep(50)
    $msg = GUIGetMsg()

    Select 
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg >= $item1 And $msg <= $item4
            $selected = _GUICtrlListView_GetSelectedIndices($listview)
            If _GUICtrlListView_GetSelectedCount($listview) = 1 Then
                MsgBox(16, "Selected Item", "There is " & _GUICtrlListView_GetSelectedCount($listview) & " ListView item selected: (index provided)" & @CRLF & @CRLF & $selected)
            Else
                MsgBox(16, "Selected Items", "There are " & _GUICtrlListView_GetSelectedCount($listview) & " ListView items selected: (indexes provided)" & @CRLF & @CRLF & $selected)
            EndIf
    EndSelect
WEnd
Link to comment
Share on other sites

au3scr

Example:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>

$hGui = GuiCreate("TestGui", 300, 200)

$hListView = GUICtrlCreateListView("Item|Subitem", 10, 10, 280, 180)

For $i = 1 To 10
    GUICtrlCreateListViewItem("Item " & $i & "|" & "SubItem " & $i, $hListView)
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $hListView
    If Not IsHWnd($hWndListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
   
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    Local $iItem = DllStructGetData($tInfo, "Item")
                    If $iItem <> -1 Then ConsoleWrite("!> Click on item " & $iItem + 1 & @LF)
            EndSwitch
    EndSwitch
   
    Return $GUI_RUNDEFMSG
EndFunc

To Moderators: Maybe attach this example to a FAQ, because similar questions appears very often?

Edited by rasim
Link to comment
Share on other sites

First, you need to store your ListItems in an array.

Then you need to decide which item was read.

Then you need to read it.

This works as "onclick"

#include <guiconstants.au3>
#include <File.au3>
#include <String.au3>
#include <GuiEdit.au3>
#include <Process.au3>
#include <GUIListView.au3>
#include <GuiConstantsEx.au3>

$Debug_LV = False; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work

Global $i
GUICreate("Window mannager", 320, 400)
$List1 = GUICtrlCreateListView ( "windows", 3,3,314,180)
_GUICtrlListView_SetColumnWidth($List1,0,310);300 is the width of the 0th column. Change it to whatever you want
$var = WinList()
Dim $arList[1];CREATE ARRAY
$arList[0]=GUICtrlCreateDummy();ISOLATE LISTVIEW ITEMS
For $i = 1 to $var[0][0]
 ; Only display visble windows that have a title
  If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then
      ReDim $arList[$i+1]
    $arList[$i]=GUICtrlCreateListViewItem($var[$i][0] , $List1)
  EndIf
Next
$i+=1
ReDim $arList[$i+1]
$arList[$i]=GUICtrlCreateDummy();ISOLATE LISTVIEW ITEMS
$selected = GUICtrlCreateInput("",3,183,314,20)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    If $nMsg > $arList[0] AND $nMsg < $arList[$i] Then;IF CONTROL IS IN THE ISOLATED LIST VIEW
        MsgBox(0,0,"You Clicked "&GUICtrlRead($nMsg))
    Else
        
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $list1
;~  MsgBox (0,0,"string from clicked label")
;### Tidy Error -> "endswitch" is closing previous "case"
        EndSwitch
;### Tidy Error -> "wend" is closing previous "switch"
    EndIf
WEnd






Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then
    Return 1
  Else
    Return 0
  EndIf

EndFunc

Good luck,

Bob

You can't see a rainbow without first experiencing the rain.

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