Sign in to follow this  
Followers 0
timor

ListView OnClick action

15 posts in this topic

Hello,

I'm writing a little app in guionevent mode and I need to get action when left clicking on listview.

I'm quite new in AutoIt so any help will be helpful.

Thanks.

Share this post


Link to post
Share on other sites



See _GUICtrlListView_Create() function example in the help file.

Share this post


Link to post
Share on other sites

One more thing... I'm using listview controls witch checkboxes and in this event it could be action on checkbox check.

Share this post


Link to post
Share on other sites

This example uses Message Loop mode, is there any way to do this in onevent mode (it's more convenient for me)?

Share this post


Link to post
Share on other sites

Example:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>

Opt("GuiOnEventMode", 1)

$hGUI = GUICreate("Test GUI", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$ListView = GUICtrlCreateListView("Items|SubItems", 10, 10, 280, 180, -1, BitOR($LVS_EX_CHECKBOXES, $WS_EX_CLIENTEDGE))

For $i = 1 To 10
    GUICtrlCreateListViewItem("Item " & $i & "|SubItem " & $i, $ListView)
    GUICtrlSetOnEvent(-1, "_GetChecked")
Next

GUISetState()

While 1
    Sleep(100)
WEnd

Func _Exit()
    Exit
EndFunc

Func _GetChecked()
    If GUICtrlRead(@GUI_CtrlId, 1) = 1 Then ConsoleWrite(GUICtrlRead(@GUI_CtrlId) & " is checked" & @LF)
EndFunc
:)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks, for that few lines. Works perfect.

One more thing. For now it works when I click on Item (using mouse), is there an easy way to get it working with keyboard actions?

Edited by timor

Share this post


Link to post
Share on other sites

Try this:

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

Opt("GuiOnEventMode", 1)

$hGUI = GUICreate("Test GUI", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$ListView = GUICtrlCreateListView("Items|SubItems", 10, 10, 280, 180, -1, BitOR($LVS_EX_CHECKBOXES, $WS_EX_CLIENTEDGE))

For $i = 1 To 10
    _GUICtrlListView_AddItem($ListView, "Item " & $i)
    _GUICtrlListView_AddSubItem($ListView, $i - 1, "SubItem " & $i, 1)
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState()

While 1
    Sleep(100)
WEnd

Func _Exit()
    Exit
EndFunc

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hListView, $tNMHDR, $hWndFrom, $iCode
    
    $hListView = $ListView
    If Not IsHWnd($hListView) Then $hListView = GUICtrlGetHandle($ListView)
    
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "HwndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $LVN_ITEMCHANGED
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    Local $iItem = DllStructGetData($tInfo, "Item")
                    
                    If _GUICtrlListView_GetItemChecked($hListView, $iItem) = True Then
                        ConsoleWrite("---> Item " & $iItem + 1 & " has checked" & @LF)
                    EndIf
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Share this post


Link to post
Share on other sites

Or this:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <Misc.au3>

Opt("GuiOnEventMode", 1)

Global $DllHandle = DllOpen("user32.dll")

$hGUI = GUICreate("Test GUI", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$ListView = GUICtrlCreateListView("Items|SubItems", 10, 10, 280, 180, -1, BitOR($LVS_EX_CHECKBOXES, $WS_EX_CLIENTEDGE))

For $i = 1 To 10
    GUICtrlCreateListViewItem("Item " & $i & "|SubItem " & $i, $ListView)
    GUICtrlSetOnEvent(-1, "_GetChecked")
Next

GUISetState()

While 1
    If _IsPressed("20", $DllHandle) Then
        _Check()
        Sleep(100)
    EndIf
    
    Sleep(100)
WEnd

Func _Exit()
    DllClose($DllHandle)
    Exit
EndFunc

Func _GetChecked()
    If GUICtrlRead(@GUI_CtrlId, 1) = 1 Then ConsoleWrite(GUICtrlRead(@GUI_CtrlId) & " is checked" & @LF)
EndFunc

Func _Check()
    Local $iItem = _GUICtrlListView_GetSelectedIndices($ListView)
    If $iItem = "" Then Return False
    
    If _GUICtrlListView_GetItemChecked($ListView, $iItem) Then ConsoleWrite("Item " & $iItem + 1 & " is checked" & @LF)
EndFunc

Share this post


Link to post
Share on other sites

Hmmm, how do you differentiate between two listviews (or more) on the same gui that have the same on-click functionality?

Share this post


Link to post
Share on other sites

nikink

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $hWndFrom, $iCode
    
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "HwndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hListView_1
            ;Some action
        Case $hListView_2
            ;Some action
        Case $hListView_3
            ;Some action
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Share this post


Link to post
Share on other sites

D'oh! Of course! I feel really dumb now... :) I've been trying to follow the examples in the help file and been caught up in the way they use $hListView all the time... didn't even occur to me to do the obvious case setup like you just posted.

Thanks! :P

Share this post


Link to post
Share on other sites

@topten take out the 1 in your call to

If GUICtrlRead(@GUI_CtrlId) = 0
See if that does the trick. I think it is giving back more information then just 0 when 1 is set to return extended info on the ctrl.

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

Ah, great! Thank you very much for such a fast reply! Yes, this way works better- allows to register if checked and unchecked

Share this post


Link to post
Share on other sites

@topten glad to help :)


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

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