Jump to content
nbaser

List view item selection event handling

Recommended Posts

Hi,

I am creating a UI in which I have to populate different data in few comboboxes depending on the item selected in ListView. Basically I want event handling when any of the item is selected.

Can anyone help me in doing this? a sample script would be of great help.

The events are not getting called in OnEvent Mode instead handled in continuous while () loop. 

Thanks in advance

Neha

Share this post


Link to post
Share on other sites

nbaser,

Her you go:

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

$Gui = GUICreate("Test", 400, 250)

$hListView = GUICtrlCreateListView("Col 0    |Col 1    ", 2, 2, 220, 196)
GUICtrlCreateListViewItem("Item 00|Item 01", $hListView)
GUICtrlCreateListViewItem("Item 10|Item 11", $hListView)
GUICtrlCreateListViewItem("Item 20|Item 21", $hListView)
GUICtrlCreateListViewItem("Item 30|Item 31", $hListView)

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch

WEnd

; This works with either type of ListView
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $wParam

    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR

    $tNMHDR = DllStructCreate("hwnd;uint_ptr;int_ptr;int;int", $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, 1))
    $iCode = DllStructGetData($tNMHDR, 3)

    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_DBLCLK
                    ConsoleWrite("Row: " & DllStructGetData($tNMHDR, 4) & " - Col: " & DllStructGetData($tNMHDR, 5) & @CRLF)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc
All clear? :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
On 9.5.2014 at 2:05 PM, Melba23 said:

    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

Despite beeing quite old, your post helped me to dig deeper into the WIndows GUI message handler, thanks very much.

I wonder why you did the two lines I quoted and introduced $hWndListView, instead of working directly with $hListView from the GUI intialization code? Would you pelase explain why the additional code is needed?

 

Thnx, Armin.

Share this post


Link to post
Share on other sites
2 minutes ago, ArminLinder said:

I wonder why you did the two lines I quoted and introduced $hWndListView, instead of working directly with $hListView from the GUI intialization code?

He does that so he's not modifying the original variable that holds the ControlID of the listview. He copied it to a new variable, then checked to see if it was a handle, and not a control ID, and if it wasn't a handle, then get the handle of it and put it into the new variable. That way the control ID stays the same and can be used in the script as is.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
1 hour ago, BrewManNH said:

He does that so he's not modifying the original variable that holds the ControlID of the listview.

The WM_NOTIFY code never assigns a value to $hListView ... how could this problem possibly occur, and if it did, why should the value written over the handle be just the Control ID of the listview??

Confused ...

Armin.

 

 

Edited by ArminLinder

Share this post


Link to post
Share on other sites

ArminLinder,

In the code I posted $hListView is the ControlID returned by the native AutoIt function GUICtrlCreateListView - I really should have used $cListView to store it. This variable is in Global scope and so is visible throughout the script. The Windows message handler needs the handle of the ListView - so those 2 lines check to see if the Global value is actually a handle (as it would be if I had created the ListView using the UDF function _GUICtrlListView_Create) and if that is not the case - retrieves the actual handle for the ControlID. Obviously I do not want to overwrite the ControlID as would be the case if I reused $hListView - it is Global in scope and so it would no longer hold the original ControlID, thus rendering it impossible to use in any functions requiring a ControlID anywhere else in the script.  In this way the handler is modular and can cope with ListViews created by either the native AutoIt or UDF functions.

The difference between ControlID and handle is fundamental to coding in AutoIt. Native functions return a ControlID - which are actually the index numbers to an internal AutoIt array which looks after the created controls. UDF  functions return a handle - the unique windows ID for all things that Windows creates. Of course the native-created controls have handles too (that is why you have the GUICtrlGetHandle function) but AutoIt tries to make life simple for you by using ControlIDs to identify its native controls when using native functions. When you look for these ControlIDs in a GUIGetMsg loop you are actually asking AutoIt to access the Windows message stream and pick put those messages which refer to that control - the OnEvent commands work in a similar manner.

I hope that clears up any questions you have, but please ask again if not.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

×
×
  • Create New...