Sign in to follow this  
Followers 0
elmauro

[SOLVED] Yet another ListView problem

3 posts in this topic

#1 ·  Posted (edited)

Another problem with listview, this time when I want to use GUICtrlRead().

If I call it (AS EXAMPLE) like this:

GUICtrlRead(GUICtrlRead($hListView))

to read item data from a "normal way created" listview it works fine.

But if I use it on a listview created from an array ( _GUICtrlListView_AddArray ), it doesnt work at all.

Two example codes.

The first one will create a listview items list using _GUICtrlListView_AddArray and then

will try to read the selected item data when mouseclick:

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

_Main()

Func _Main()
    Local $iI, $iTimer, $hListView

    ; Create GUI
    GUICreate("ListView Add Array", 400, 300)
    $hListView = GUICtrlCreateListView("", 2, 2, 394, 268)
    _GUICtrlListView_SetUnicodeFormat($hListView, False)
    GUISetState()

    ; Add columns
    _GUICtrlListView_AddColumn($hListView, "Items", 100)
    _GUICtrlListView_AddColumn($hListView, "SubItems 1", 100)
    _GUICtrlListView_AddColumn($hListView, "SubItems 2", 100)
    _GUICtrlListView_AddColumn($hListView, "SubItems 3", 100)

    _GUICtrlListView_SetItemCount($hListView, 30)
    
    ; Four column load
    Dim $aItems[30][4]
    For $iI = 0 To UBound($aItems) - 1
        $aItems[$iI][0] = "Item " & $iI
        $aItems[$iI][1] = "Item " & $iI & "-1"
        $aItems[$iI][2] = "Item " & $iI & "-2"
        $aItems[$iI][3] = "Item " & $iI & "-3"
    Next
    _GUICtrlListView_AddArray($hListView, $aItems)

    ; Loop until user exits
    While 1
     $gmsg = GUIGetMsg()
     Switch $gmsg
     case $GUI_EVENT_CLOSE
         Exit
         
     case $GUI_EVENT_PRIMARYDOWN
        $cItem = GUICtrlRead(GUICtrlRead($hListView))
        MsgBox(0,"", $cItem)
     EndSwitch
 WEnd
 
EndFunc   ;==>_Main

The second one, will create the items list using GUICtrlCreateListViewItem and

then will do the same as first one but sucefull.

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

_Main()

Func _Main()
    Local $iI, $iTimer, $hListView

    ; Create GUI
    GUICreate("ListView Add Array", 400, 300)
    $hListView = GUICtrlCreateListView("", 2, 2, 394, 268)
    _GUICtrlListView_SetUnicodeFormat($hListView, False)
    GUISetState()

    ; Add columns
    _GUICtrlListView_AddColumn($hListView, "Items", 100)
    _GUICtrlListView_AddColumn($hListView, "SubItems 1", 100)
    _GUICtrlListView_AddColumn($hListView, "SubItems 2", 100)
    _GUICtrlListView_AddColumn($hListView, "SubItems 3", 100)

    _GUICtrlListView_SetItemCount($hListView, 30)
    
    ; Four column load
    
    For $iI = 0 To 30
        GUICtrlCreateListViewItem("Item " & $iI & "|" & "Item " & $iI & "-1" & "|" & _
        "Item " & $iI & "-2" & "|" & "Item " & $iI & "-3" & "|" , $hListView)
    Next

    ; Loop until user exits
    While 1
     $gmsg = GUIGetMsg()
     Switch $gmsg
     case $GUI_EVENT_CLOSE
         Exit
         
     case $GUI_EVENT_PRIMARYDOWN
        $cItem = GUICtrlRead(GUICtrlRead($hListView))
        MsgBox(0,"", $cItem)
     EndSwitch
 WEnd
 
EndFunc   ;==>_Main

Im so doomed.

Why this happens?

Edited by elmauro

Share this post


Link to post
Share on other sites



GUICtrlRead requires control-id's, which are created when using GUICtrlCreateListView but not when using _GUICtrlListView_AddArray.

This will give the same result for _GUICtrlListView_AddArray

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>

Global $fHit = False, $aHit, $hListView

_Main()

Func _Main()
    Local $idListView, $cItem

    ; Create GUI
    GUICreate("ListView Add Array", 400, 300)
    $idListView = GUICtrlCreateListView("", 2, 2, 394, 268)
    $hListView = GUICtrlGetHandle($idListView)
    _GUICtrlListView_SetUnicodeFormat($hListView, False)

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

    ; Add columns
    _GUICtrlListView_AddColumn($hListView, "Items", 100)
    _GUICtrlListView_AddColumn($hListView, "SubItems 1", 100)
    _GUICtrlListView_AddColumn($hListView, "SubItems 2", 100)
    _GUICtrlListView_AddColumn($hListView, "SubItems 3", 100)
    _GUICtrlListView_SetItemCount($hListView, 30)

    ; Four column load
    Local $aItems[30][4]
    For $iI = 0 To UBound($aItems) - 1
        $aItems[$iI][0] = "Item " & $iI
        $aItems[$iI][1] = "Item " & $iI & "-1"
        $aItems[$iI][2] = "Item " & $iI & "-2"
        $aItems[$iI][3] = "Item " & $iI & "-3"
    Next
    _GUICtrlListView_AddArray($hListView, $aItems)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
;~             Case $GUI_EVENT_PRIMARYDOWN
;~                 $cItem = GUICtrlRead(GUICtrlRead($idListView))    ; only works with items added via GUICtrlCreateListViewItem which  return a control-id
;~                 MsgBox(262144, "", $cItem)
        EndSwitch
        If $fHit Then
            $cItem = ""
            For $i = 0 To _GUICtrlListView_GetColumnCount($hListView) - 1
                $cItem &= _GUICtrlListView_GetItemText($hListView, $aHit[0], $i) & "|"
            Next
            ;ConsoleWrite($cItem & @LF)
            MsgBox(262144, "", $cItem)
            $fHit = False
        EndIf
    WEnd
EndFunc   ;==>_Main

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    ;$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_CLICK ; $NM_DBLCLK
                    $aHit = _GUICtrlListView_SubItemHitTest($hWndFrom)
                    If $aHit[0] <> -1 Then $fHit = True
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Share this post


Link to post
Share on other sites

Thanks man that worked great.

I just was trying to replace the "listview item replenish" mode from

adding on the fly to building an array and then adding the array because

with larger results it looks better (not that glitchy cascade effect).

But now I see that would require alot more functions so I will stay with the

old style atm.

Thanks again for teaching me ! :D

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  
Followers 0