Jump to content
Sign in to follow this  

Index of newly-added listview item?

Recommended Posts


Hi All,

I'm sure there's a simple answer to this using BitAnd or BitOr, but I can't seem to find it.

I have created a listview, and added listview items to it. This works, and the first entry returns an index of 4108 when using GUICtrlRead.

The problem I am having is that after the initial set-up of the listview, if I add another item, the index number jumps by more than 1 when I use GUICtrlRead to get the index number.

Any ideas on how to fix this?

The listview is created without any styles or exstyles, as are the listviewitems.

Share this post

Link to post
Share on other sites

Hi! Example:

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

$Gui = GUICreate("Test", 300, 200)

$hListView = _GUICtrlListView_Create($GUI, "Items|SubItems", 20, 15, 260, 150, BitOR($LVS_EDITLABELS, $LVS_REPORT), $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_SUBITEMIMAGES)

_GUICtrlListView_AddItem($hListView, "Item1")
_GUICtrlListView_AddItem($hListView, "Item2")
_GUICtrlListView_AddItem($hListView, "Item3")
_GUICtrlListView_AddItem($hListView, "Item4")

$AddButton = GUICtrlCreateButton("Add", 20, 172, 50, 20)



While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $AddButton

Func _InsertItem()
    Local $iIndex = _GUICtrlListView_GetItemCount($hListView)
    _GUICtrlListView_InsertItem($hListView, "Item" & $iIndex + 1)
EndFunc   ;==>_InsertItem

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView
    $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 $LVN_INSERTITEM
            Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
            Local $iIndex = DllStructGetData($tInfo, "Item")
            ConsoleWrite("Added new item with index #" & $iIndex & @LF)
:) Edited by rasim

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.