Jump to content

ListView OnClick action


timor
 Share

Recommended Posts

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
:)
Link to comment
Share on other sites

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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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

Link to comment
Share on other sites

  • 6 years later...

@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

 

Link to comment
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

 

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