Jump to content
Sign in to follow this  
osirisqc

GUICtrlCreateListView create event when selecting and item

Recommended Posts

osirisqc

hi all!

i want to create an event when clicking on an item...

i tried GUICtrlSetOnEvent but the event occur only when i click on a column, i want the event happens when i click on an item in the list (texts)

... and how to settle the event on a single click, how on dblclick???

because i have an event for single click and a different event for double clic...

thanks for your help!

Share this post


Link to post
Share on other sites
Zedna

#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>

$Form1 = GUICreate("Test", 300, 200, -1, -1)
$ListView1 = GUICtrlCreateListView("A|B|C", 15, 15, 270, 118)
GUICtrlCreateListViewItem("r1c1|r1c2|r1c3", $ListView1)
GUICtrlCreateListViewItem("r2c1|r2c2|r2c3", $ListView1)
_GUICtrlListView_SetColumnWidth($ListView1, 0, $LVSCW_AUTOSIZE)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

While 1
    $msg = GuiGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd

Func OnClick($item, $subitem)
    $value = _GUICtrlListView_GetItemText($ListView1,$item,$subitem)
    ConsoleWrite('Click: index:' & $item & ' subitem:' & $subitem & ' value:' & $value & @CRLF)
EndFunc

Func OnDoubleClick($item, $subitem)
    $value = _GUICtrlListView_GetItemText($ListView1,$item,$subitem)
    ConsoleWrite('DoubleClick: index:' & $item & ' subitem:' & $subitem & ' value:' & $value & @CRLF)
EndFunc

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam

    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    Local $nNotifyCode = DllStructGetData($tNMHDR, "Code")

    If $wParam = $ListView1 Then
        If $nNotifyCode = $NM_CLICK Then
            $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
            $item = DllStructGetData($tInfo, "Index")
            $subitem = DllStructGetData($tInfo, "SubItem")
            OnClick($item,$subitem)
        EndIf

        If $nNotifyCode = $NM_DBLCLK Then
            $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
            $item = DllStructGetData($tInfo, "Index")
            $subitem = DllStructGetData($tInfo, "SubItem")
            OnDoubleClick($item,$subitem)
        EndIf
    EndIf
EndFunc

Share this post


Link to post
Share on other sites
Zedna

And here is the same but non-blocking, so here you can use also time consuming (long lasting) code:

#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>

$Form1 = GUICreate("Test", 300, 200, -1, -1)
$ListView1 = GUICtrlCreateListView("A|B|C", 15, 15, 270, 118)
GUICtrlCreateListViewItem("r1c1|r1c2|r1c3", $ListView1)
GUICtrlCreateListViewItem("r2c1|r2c2|r2c3", $ListView1)
_GUICtrlListView_SetColumnWidth($ListView1, 0, $LVSCW_AUTOSIZE)
$click_id = GuiCtrlCreateDummy()
$dblclick_id = GuiCtrlCreateDummy()
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

While 1
    $msg = GuiGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $click_id
            OnClick(GUICtrlRead($click_id))
        Case $msg = $dblclick_id
            OnDoubleclick(GUICtrlRead($dblclick_id))
    EndSelect
WEnd

Func OnClick($subitem)
    $item = _GUICtrlListView_GetNextItem($ListView1) ; current selected
    $value = _GUICtrlListView_GetItemText($ListView1,$item,$subitem)
    ConsoleWrite('Click: index:' & $item & ' subitem:' & $subitem & ' value:' & $value & @CRLF)
EndFunc

Func OnDoubleClick($subitem)
    $item = _GUICtrlListView_GetNextItem($ListView1) ; current selected
    $value = _GUICtrlListView_GetItemText($ListView1,$item,$subitem)
    ConsoleWrite('DoubleClick: index:' & $item & ' subitem:' & $subitem & ' value:' & $value & @CRLF)
EndFunc

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam

    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    Local $nNotifyCode = DllStructGetData($tNMHDR, "Code")

    If $wParam = $ListView1 Then
        If $nNotifyCode = $NM_CLICK Then
            $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
;~             $item = DllStructGetData($tInfo, "Index")
            $subitem = DllStructGetData($tInfo, "SubItem")
;~             OnClick($item,$subitem)
            GUICtrlSendToDummy($click_id, $subitem)
        EndIf

        If $nNotifyCode = $NM_DBLCLK Then
            $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
;~             $item = DllStructGetData($tInfo, "Index")
            $subitem = DllStructGetData($tInfo, "SubItem")
;~             OnDoubleClick($item,$subitem)
            GUICtrlSendToDummy($dblclick_id, $subitem)
        EndIf
    EndIf
EndFunc
  • Like 1

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  

×