Jump to content

Trouble with ListView on click


Jewtus
 Share

Go to solution Solved by Melba23,

Recommended Posts

I have been trying to figure out how to trigger events based on the item selected from a list view.

I've found some sample scripts which I was able to adapt, but I can only figure out how to get it to write the value I'm looking for to the console. Here is a sample:

#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>
#include <ComboConstants.au3>
#include <WinApi.au3>
#include <GuiListView.au3>

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

EditAnswerGUI()

Func EditAnswerGUI()
    $AnswerGUI = GUICreate("Edit UI", 804, 462, 179, 104)
    global $QAList = GUICtrlCreateListView("", 7, 8, 785, 201)
    _GUICtrlListView_InsertColumn($QAList, 0, "Field I", 110)
    _GUICtrlListView_InsertColumn(-1, 1, "Field II", 100)
    _GUICtrlListView_InsertColumn(-1, 2, "Field III", 100)
    _GUICtrlListView_InsertColumn(-1, 3, "Field IV", 100)
    _GUICtrlListView_InsertColumn(-1, 4, "Field V", 100)
    _GUICtrlListView_InsertColumn(-1, 5, "Hyperlink", 100)
    GUICtrlCreateListViewItem('This|Is|A|Test|Now', $QAList)
    GUICtrlCreateListViewItem('This1|Is1|A1|Test1|Now1', $QAList)
    $BLInput = GUICtrlCreateInput("", 8, 232, 250, 21)
    $IssueInput = GUICtrlCreateInput("", 276, 232, 250, 21)
    $IssueDescriptionInput = GUICtrlCreateInput("", 543, 232, 250, 21)
    $Category1 = GUICtrlCreateLabel("Field I", 8, 215, 52, 17)
    $Category2 = GUICtrlCreateLabel("Field II", 276, 215, 55, 17)
    $Category3 = GUICtrlCreateLabel("Field III", 544, 214, 58, 17)
    $QuestionLabel = GUICtrlCreateLabel("Field IV", 8, 258, 46, 17)
    $QuestionInput = GUICtrlCreateInput("", 8, 275, 785, 21)
    $Answer = GUICtrlCreateLabel("Field V", 9, 299, 39, 17)
    $AnswerBox = GUICtrlCreateEdit("", 7, 317, 785, 97)
    $Update = GUICtrlCreateButton("Update", 712, 424, 75, 25)
    GUISetState(@SW_SHOW)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $QAList
;               $selected= StringSplit(_GUICtrlListView_GetItemTextString($QAList), "|", 2)[0]
                $selected= _GUICtrlListView_GetItemTextString($QAList)
                _ArrayDisplay($selected)
        EndSwitch
    WEnd
EndFunc


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

    $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)
                    ConsoleWrite(DllStructGetData($tInfo, "Index") & @LF)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

I don't actually understand fully how the WM_Notify function actually works, but it is spitting out the index of the row, which is what I need.

Link to comment
Share on other sites

  • Moderators

Jewtus,

So what is the question: how does the WM_NOTIFY code work, or how to get the content of the selected row? :huh:

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

Lets start with how to get the content. Once I have a model I can usually figure it out, but I'm pretty lost on it without context. I would rather not use a func to do this but put it right in line as a case statement (case the List view is selected, and it automatically sets the data into the fields of the GUI), but from what I've read, I can't. I know how to do this with a button, but I'm trying to avoid having to click a record and then click a button.

Link to comment
Share on other sites

  • Moderators
  • Solution

Jewtus,

 

how to get the content

Just use the function correctly inside the handler and use the correct method to display the returned data: :)

#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>
#include <ComboConstants.au3>
#include <WinApi.au3>
#include <GuiListView.au3>

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

EditAnswerGUI()

Func EditAnswerGUI()
    $AnswerGUI = GUICreate("Edit UI", 804, 462, 179, 104)
    global $QAList = GUICtrlCreateListView("", 7, 8, 785, 201)
    _GUICtrlListView_InsertColumn($QAList, 0, "Field I", 110)
    _GUICtrlListView_InsertColumn(-1, 1, "Field II", 100)
    _GUICtrlListView_InsertColumn(-1, 2, "Field III", 100)
    _GUICtrlListView_InsertColumn(-1, 3, "Field IV", 100)
    _GUICtrlListView_InsertColumn(-1, 4, "Field V", 100)
    _GUICtrlListView_InsertColumn(-1, 5, "Hyperlink", 100)
    GUICtrlCreateListViewItem('This|Is|A|Test|Now', $QAList)
    GUICtrlCreateListViewItem('This1|Is1|A1|Test1|Now1', $QAList)
    $BLInput = GUICtrlCreateInput("", 8, 232, 250, 21)
    $IssueInput = GUICtrlCreateInput("", 276, 232, 250, 21)
    $IssueDescriptionInput = GUICtrlCreateInput("", 543, 232, 250, 21)
    $Category1 = GUICtrlCreateLabel("Field I", 8, 215, 52, 17)
    $Category2 = GUICtrlCreateLabel("Field II", 276, 215, 55, 17)
    $Category3 = GUICtrlCreateLabel("Field III", 544, 214, 58, 17)
    $QuestionLabel = GUICtrlCreateLabel("Field IV", 8, 258, 46, 17)
    $QuestionInput = GUICtrlCreateInput("", 8, 275, 785, 21)
    $Answer = GUICtrlCreateLabel("Field V", 9, 299, 39, 17)
    $AnswerBox = GUICtrlCreateEdit("", 7, 317, 785, 97)
    $Update = GUICtrlCreateButton("Update", 712, 424, 75, 25)
    GUISetState(@SW_SHOW)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd
EndFunc


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

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    ; We need the index because the click has not yet changed the selection
                    $iIndex = DllStructGetData($tInfo, "Index")
                    $sSelected= _GUICtrlListView_GetItemTextString($QAList, $iIndex) ; This returns a string, not an array
                    ConsoleWrite($sSelected & @CRLF)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Or set a flag in the handler and put the code in the idle loop - a good idea if there is a lot of it or you use a blocking function: ;)

#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>
#include <ComboConstants.au3>
#include <WinApi.au3>
#include <GuiListView.au3>

; Declare flag
Global $bClick = False

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

EditAnswerGUI()

Func EditAnswerGUI()
    $AnswerGUI = GUICreate("Edit UI", 804, 462, 179, 104)
    global $QAList = GUICtrlCreateListView("", 7, 8, 785, 201)
    _GUICtrlListView_InsertColumn($QAList, 0, "Field I", 110)
    _GUICtrlListView_InsertColumn(-1, 1, "Field II", 100)
    _GUICtrlListView_InsertColumn(-1, 2, "Field III", 100)
    _GUICtrlListView_InsertColumn(-1, 3, "Field IV", 100)
    _GUICtrlListView_InsertColumn(-1, 4, "Field V", 100)
    _GUICtrlListView_InsertColumn(-1, 5, "Hyperlink", 100)
    GUICtrlCreateListViewItem('This|Is|A|Test|Now', $QAList)
    GUICtrlCreateListViewItem('This1|Is1|A1|Test1|Now1', $QAList)
    $BLInput = GUICtrlCreateInput("", 8, 232, 250, 21)
    $IssueInput = GUICtrlCreateInput("", 276, 232, 250, 21)
    $IssueDescriptionInput = GUICtrlCreateInput("", 543, 232, 250, 21)
    $Category1 = GUICtrlCreateLabel("Field I", 8, 215, 52, 17)
    $Category2 = GUICtrlCreateLabel("Field II", 276, 215, 55, 17)
    $Category3 = GUICtrlCreateLabel("Field III", 544, 214, 58, 17)
    $QuestionLabel = GUICtrlCreateLabel("Field IV", 8, 258, 46, 17)
    $QuestionInput = GUICtrlCreateInput("", 8, 275, 785, 21)
    $Answer = GUICtrlCreateLabel("Field V", 9, 299, 39, 17)
    $AnswerBox = GUICtrlCreateEdit("", 7, 317, 785, 97)
    $Update = GUICtrlCreateButton("Update", 712, 424, 75, 25)
    GUISetState(@SW_SHOW)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch

        ; If flag set
        If $bClick Then
            ; Clear flag
            $bClick = False
            ; Run code - no need for the index as the click has now selected the item
            $sSelected= _GUICtrlListView_GetItemTextString($QAList, -1)
            ConsoleWrite($sSelected & @CRLF)
        EndIf

    WEnd
EndFunc


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

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                   $bClick = True ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
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

A slightly more simplified approach...

#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>
#include <ComboConstants.au3>
#include <WinApi.au3>
#include <GuiListView.au3>

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

Global $QAlist, $lv_dummy ; define globals outside of functions

EditAnswerGUI()

Func EditAnswerGUI()

    local $aTmp

    $AnswerGUI = GUICreate("Edit UI", 804, 462, 179, 104)
    $QAlist = GUICtrlCreateListView("", 7, 8, 785, 201)
    _GUICtrlListView_InsertColumn($QAlist, 0, "Field I", 110)
    _GUICtrlListView_InsertColumn(-1, 1, "Field II", 100)
    _GUICtrlListView_InsertColumn(-1, 2, "Field III", 100)
    _GUICtrlListView_InsertColumn(-1, 3, "Field IV", 100)
    _GUICtrlListView_InsertColumn(-1, 4, "Field V", 100)
    _GUICtrlListView_InsertColumn(-1, 5, "Hyperlink", 100)
    GUICtrlCreateListViewItem('This|Is|A|Test|Now', $QAlist)
    GUICtrlCreateListViewItem('This1|Is1|A1|Test1|Now1', $QAlist)
    $BLInput = GUICtrlCreateInput("", 8, 232, 250, 21)
    $IssueInput = GUICtrlCreateInput("", 276, 232, 250, 21)
    $IssueDescriptionInput = GUICtrlCreateInput("", 543, 232, 250, 21)
    $Category1 = GUICtrlCreateLabel("Field I", 8, 215, 52, 17)
    $Category2 = GUICtrlCreateLabel("Field II", 276, 215, 55, 17)
    $Category3 = GUICtrlCreateLabel("Field III", 544, 214, 58, 17)
    $QuestionLabel = GUICtrlCreateLabel("Field IV", 8, 258, 46, 17)
    $QuestionInput = GUICtrlCreateInput("", 8, 275, 785, 21)
    $Answer = GUICtrlCreateLabel("Field V", 9, 299, 39, 17)
    $AnswerBox = GUICtrlCreateEdit("", 7, 317, 785, 97)
    $Update = GUICtrlCreateButton("Update", 712, 424, 75, 25)
    $lv_dummy = GUICtrlCreateDummy() ; control to be actioned by Notify routine
    GUISetState(@SW_SHOW)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $lv_dummy

                ConsoleWrite(_GUICtrlListView_GetItemTextString($QAlist, -1) & @LF) ;   write delimited selected item to console

                ; this appears to be what you want to do
                local $aTmp = stringsplit(_GUICtrlListView_GetItemTextString($QAlist, -1),'|',2)
                guictrlsetdata($BLInput,$aTmp[0])
                guictrlsetdata($IssueInput,$aTmp[1])
                guictrlsetdata($IssueDescriptionInput,$aTmp[2])
                guictrlsetdata($QuestionInput,$aTmp[3])
                guictrlsetdata($AnswerBox,$aTmp[4])
        EndSwitch
    WEnd
EndFunc   ;==>EditAnswerGUI

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    Switch $tNMHDR.hwndfrom
        Case GUICtrlGetHandle($QAlist) ; create handle from control id (native control only, UDF controls create handles)
            Switch $tNMHDR.Code
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    GUICtrlSendToDummy($lv_dummy) ; process listview by actioning dummy control
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Thanks guys!

I ended up going with:

#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>
#include <ComboConstants.au3>
#include <WinApi.au3>
#include <GuiListView.au3>

; Declare flag
Global $bClick = False

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

EditAnswerGUI()

Func EditAnswerGUI()
    $AnswerGUI = GUICreate("Edit UI", 804, 500, 179, 104)
    global $QAList = GUICtrlCreateListView("", 7, 8, 785, 201)
    _GUICtrlListView_InsertColumn($QAList, 0, "Field I", 110)
    _GUICtrlListView_InsertColumn(-1, 1, "Field II", 100)
    _GUICtrlListView_InsertColumn(-1, 2, "Field III", 100)
    _GUICtrlListView_InsertColumn(-1, 3, "Field IV", 100)
    _GUICtrlListView_InsertColumn(-1, 4, "Field V", 100)
    _GUICtrlListView_InsertColumn(-1, 5, "Hyperlink", 100)
    GUICtrlCreateListViewItem('This|Is|A|Test|Now', $QAList)
    GUICtrlCreateListViewItem('This1|Is1|A1|Test1|Now1', $QAList)
    $BLInput = GUICtrlCreateLabel("", 8, 232, 250, 21)
    $IssueInput = GUICtrlCreateLabel("", 276, 232, 250, 21)
    $IssueDescriptionInput = GUICtrlCreateLabel("", 543, 232, 250, 21)
    $Category1 = GUICtrlCreateLabel("Category I", 8, 215, 52, 17)
    $Category2 = GUICtrlCreateLabel("Category II", 276, 215, 55, 17)
    $Category3 = GUICtrlCreateLabel("Category III", 544, 214, 58, 17)
    $QuestionLabel = GUICtrlCreateLabel("Question", 8, 258, 46, 17)
    $QuestionInput = GUICtrlCreateInput("", 8, 275, 785, 21)
    $Answer = GUICtrlCreateLabel("Answer", 9, 299, 39, 17)
    $AnswerBox = GUICtrlCreateEdit("", 7, 317, 785, 97)
    $HyperlinkLabel = GUICtrlCreateLabel("Question", 8, 420, 46, 17)
    $HyperlinkInput = GUICtrlCreateInput("", 8, 440, 785, 21)
    $Update = GUICtrlCreateButton("Update", 712, 470, 75, 25)
    GUISetState(@SW_SHOW)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
        ; If flag set
        If $bClick Then
            ; Clear flag
            $bClick = False
            ; Run code - no need for the index as the click has now selected the item
            $sSelected= _GUICtrlListView_GetItemTextString($QAList, -1)
            $sSelected= _GUICtrlListView_GetItemTextString($QAList, -1)
            $form=StringSplit($sSelected,"|")
            If $form[1] <> '' Then
                _ArrayDisplay($form)
                GUICtrlSetData($BLInput,$form[1])
                GUICtrlSetData($IssueInput,$form[2])
                GUICtrlSetData($IssueDescriptionInput,$form[3])
                GUICtrlSetData($QuestionInput,$form[4])
                GUICtrlSetData($AnswerBox,$form[5])
                GUICtrlSetData($HyperlinkInput,$form[6])
            EndIf
        EndIf
    WEnd
EndFunc


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $QAlist
    If Not IsHWnd($QAlist) Then $hWndListView = GUICtrlGetHandle($QAlist)
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                   $bClick = True ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
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...