Jump to content

How to detect which listview was clicked


Beornhardt
 Share

Go to solution Solved by Melba23,

Recommended Posts

Hi there!

I'm new to this forum, and quite new to AutoIt.

I created a GUI with two ListViews on it, whose Lines should be clickable.

Now, here's my question: How do I detect, which ListView was clicked on, and from this ListView, which element was clicked?

I already figured out how to work out the click on one of the Items in the first ListView...

(Posting the whole code would be a bit much, as it is about 5000 Lines already... ;) )

Thanks in advance for your efforts

B.

Link to comment
Share on other sites

  • Moderators
  • Solution

Beornhardt,

Welcome to the AutoIt forum. :)

I do not recommend looking for single clicks on ListViews as in my experience they are often eaten by the control itself - I always suggest looking for doubleclicks. ;)

And this is how you might do it: :)

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

Global $fClick = 0, $iLV, $iRow, $iCol

$hGUI = GUICreate("Test", 500, 500)

$cLV_1 = GUICtrlCreateListView("", 10, 10, 230, 300)
_GUICtrlListView_AddColumn($cLV_1, "Col 0", 100)
_GUICtrlListView_AddColumn($cLV_1, "Col 1", 100)
For $i = 0 To 25
    GUICtrlCreateListViewItem("Item 1" & $i & "0|Item 1" & $i & "1", $cLV_1)
Next
$hLV_1_Handle = GUICtrlGetHandle($cLV_1)

$cLV_2 = GUICtrlCreateListView("", 260, 10, 230, 300)
_GUICtrlListView_AddColumn($cLV_2, "Col 0", 100)
_GUICtrlListView_AddColumn($cLV_2, "Col 1", 100)
For $i = 0 To 25
    GUICtrlCreateListViewItem("Item 2" & $i & "0|Item 2" & $i & "1", $cLV_2)
Next
$hLV_2_Handle = GUICtrlGetHandle($cLV_2)

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY_Handler")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    
    ; Look for click flag
    If $fClick Then
        ; Display result
        MsgBox($MB_SYSTEMMODAL, "Clicked", "LV:" & @TAB & $iLV & @CRLF & "Row:" & @TAB & $iRow & @CRLF & "Col:" & @TAB & $iCol)
        ; Clear flag
        $fClick = 0
    EndIf

WEnd

Func _WM_NOTIFY_Handler($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $wParam

    ; Struct = $tagNMHDR and "int Item;int SubItem" from $tagNMLISTVIEW
    Local $tStruct = DllStructCreate("hwnd;uint_ptr;int_ptr;int;int", $lParam)
    If @error Then Return

    Switch DllStructGetData($tStruct, 1)
        Case $hLV_1_Handle
            $iLV = 1
        Case $hLV_2_Handle
            $iLV = 2
        Case Else
            Return
    EndSwitch

    If BitAND(DllStructGetData($tStruct, 3), 0xFFFFFFFF) = $NM_DBLCLK Then
        $iRow = DllStructGetData($tStruct, 4)
        $iCol = DllStructGetData($tStruct, 5)
        ; Set flag
        $fClick = 1
    EndIf

EndFunc
I realise this is not exactly a simple script - so please ask if you have any questions. :)

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

 

Link to comment
Share on other sites

  • Moderators

Beornhardt,

The struct has 5 components - these are referenced from 1 to 5. So 1 refers to the hWnd section, as shown by the fact that I check it against the handles of the ListViews. Similarly, the item/subitem are in sections 4 and 5 (the 2 integer values at the end). :)

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

 

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