Jump to content

Call Function when Item is selected in ListView


Recommended Posts

Is there a Windows Message Code I can use with GUIRegisterMsg() to execute a function when an item in a ListView is selected?

I have used $WM_MOUSEACTIVE which kinda works but only with a double-click but not a single-click.

#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

GUICreate("", 500, 500)
    Dim $hList = GUICtrlCreateListView("List Items", 10, 10, 480, 450)
    Dim $hInput = GUICtrlCreateInput("", 10, 470, 480, 20)

    For $x = 0 To 20
        GUICtrlCreateListViewItem("List Item No. " & $x, $hList)
    Next

    _GUICtrlListView_SetColumnWidth($hList, 0, 470)

GUISetState()
GUIRegisterMsg($WM_MOUSEACTIVATE, "ListSelect")

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func ListSelect()
    Local $hCount = _GUICtrlListView_GetItemCount($hList)
    For $y = 0 to $hCount
        If _GUICtrlListView_GetItemSelected($hList, $y) = True Then GUICtrlSetData($hInput, _GUICtrlListView_GetItemText($hList, $y))
    Next
EndFunc
Link to comment
Share on other sites

Try this:

#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

GUICreate("", 500, 500)
    Global $hListView = GUICtrlCreateListView("List Items", 10, 10, 480, 450)
    Global $hInput = GUICtrlCreateInput("", 10, 470, 480, 20)

    For $x = 0 To 20
        GUICtrlCreateListViewItem("List Item No. " & $x, $hListView)
    Next

    _GUICtrlListView_SetColumnWidth($hListView, 0, 470)

GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func Action($index)
    MsgBox(0, "Information", _GUICtrlListView_GetItemText($hListView, $index))
EndFunc

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    Action(DllStructGetData($tInfo, "Index"))
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thank you for the quick response and this does work well but wondering what and why is there so much code in the WM_NOTIFY function?

I do not know what all this is doing but I can enter a check like: If GUICtrlGetHandle($GuiListView) <> 0 Then .... and it works just the same?

Link to comment
Share on other sites

It does? You should find that a control handle is <> 0 at any time, not just when it has been clicked.

The check you may be looking at is just to accommodate inexperienced users that don't know the difference between a ControlID and the HWND, so they tend to pass the former where the latter is required. Most UDFs guard against this mistake in that fashion.

:unsure:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...