Jump to content

Deleting listview by index returned by _GUICtrlListView_AddItem()


Recommended Posts

Hello friends, I am trying to create an listview which is updated from time to time to show the online/offline status of a user.

I am trying to delete items in the listview by the index returned by _GUICtrlListView_AddItem() however this doesn't seem to work.

Instead I'm having to use _GUICtrlListView_FindInText()

Below is the snippet of code I would like to get to work, however for some reason it's not working. What am I doing wrong?

Func _getcontacts()

        $oHTTP.Open("GET", "https://pdglobal.net?sid=contactslist&session=" & $session, False)
        $oHTTP.Send()
        $contacts = $oHTTP.ResponseText
    $clist = StringSplit(StringTrimLeft($contacts, 1), "|")
    $index = 0

    _GUICtrlListBox_ResetContent($List2)
    For $i = 1 To $clist[0]
        If $clist[$i] <> "" Then
            $conversations[0][0] += 1
            $conversations[$i][0] = $clist[$i]
            $conversations[$i][2] = _bang(FileRead(IniRead(@ScriptDir & "\settings.ini", "userkeys", $clist[$i], "NA")))
            $conversations[$i][3] = 0
            $conversations[$i][4] = _GUICtrlListView_AddItem($List2, $clist[$i], 1) ;store the index here
            $index += 1
        EndIf
    Next
EndFunc   ;==>_getcontacts

func _getonlinestatus()
$index_old = _GUICtrlListView_GetHotItem($list2)

    for $i = 1 to $conversations[0][0]
        $oHTTP.Open("GET", "https://pdglobal.net?sid=getonlinestatus&USER="&$conversations[$i][0], False)
        $oHTTP.Send()
        $online = $oHTTP.ResponseText
        ConsoleWrite($conversations[$i][0]&"-"&$online&";")
        if $online = 1 Then
            _GUICtrlListView_BeginUpdate($list2)
            _GUICtrlListView_DeleteItem($list2,$conversations[$i][4]);delete the index here
            if $conversations[$i][3] > 0 Then
                $conversations[$i][4] = _GUICtrlListView_AddItem($list2, $conversations[$i][0]&" ("&$conversations[$i][3]&")", 0)
            Else
                $conversations[$i][4] = _GUICtrlListView_AddItem($list2, $conversations[$i][0], 0)
            EndIf
            _GUICtrlListView_EndUpdate($list2)
        Else
            _GUICtrlListView_BeginUpdate($list2)
            _GUICtrlListView_DeleteItem($list2,$conversations[$i][4]);delete the index here
            if $conversations[$i][3] > 0 Then
                $conversations[$i][4] = _GUICtrlListView_AddItem($list2, $conversations[$i][0]&" ("&$conversations[$i][3]&")", 1)
            Else
                $conversations[$i][4] = _GUICtrlListView_AddItem($list2, $conversations[$i][0], 1)
            EndIf
            _GUICtrlListView_EndUpdate($list2)
        EndIf

    Next
    if $index_old <> -1 Then
    ;_GUICtrlListBox_ClickItem($list2, $index_old)
    _GUICtrlListView_SetHotItem($list2, $index_old)
    EndIf
    ConsoleWrite(@CRLF)

EndFunc

 

Link to comment
Share on other sites

Figured out how to make it work

 

Func _getcontacts()

        $oHTTP.Open("GET", "https://pdglobal.net?sid=contactslist&session=" & $session, False)
        $oHTTP.Send()
        $contacts = $oHTTP.ResponseText
    $clist = StringSplit(StringTrimLeft($contacts, 1), "|")
    $index = 0

    _GUICtrlListBox_ResetContent($List2)
    For $i = 1 To $clist[0]
        If $clist[$i] <> "" Then
            $conversations[0][0] += 1
            $conversations[$i][0] = $clist[$i]
            $conversations[$i][2] = _bang(FileRead(IniRead(@ScriptDir & "\settings.ini", "userkeys", $clist[$i], "NA")))
            $conversations[$i][3] = 0
            $conversations[$i][4] = _GUICtrlListView_AddItem($List2, $clist[$i], 1)
            $index += 1
        EndIf
    Next
EndFunc   ;==>_getcontacts

func _getonlinestatus()
$index_old = _GUICtrlListView_GetHotItem($list2)

    for $i = 1 to $conversations[0][0]
        $oHTTP.Open("GET", "https://pdglobal.net?sid=getonlinestatus&USER="&$conversations[$i][0], False)
        $oHTTP.Send()
        $online = $oHTTP.ResponseText
        ConsoleWrite($conversations[$i][0]&"-"&$online&";")
        if $online = 1 Then
            _GUICtrlListView_BeginUpdate($list2)
            _GUICtrlListView_DeleteItem($list2, _GUICtrlListView_MapIDToIndex($list2, $conversations[$i][4]))
            if $conversations[$i][3] > 0 Then
                $conversations[$i][4] = _GUICtrlListView_AddItem($list2, $conversations[$i][0]&" ("&$conversations[$i][3]&")", 0)
            Else
                $conversations[$i][4] = _GUICtrlListView_AddItem($list2, $conversations[$i][0], 0)
            EndIf
            _GUICtrlListView_EndUpdate($list2)
        Else
            _GUICtrlListView_BeginUpdate($list2)
            _GUICtrlListView_DeleteItem($list2,_GUICtrlListView_MapIDToIndex($list2, $conversations[$i][4]))
            if $conversations[$i][3] > 0 Then
                $conversations[$i][4] = _GUICtrlListView_AddItem($list2, $conversations[$i][0]&" ("&$conversations[$i][3]&")", 1)
            Else
                $conversations[$i][4] = _GUICtrlListView_AddItem($list2, $conversations[$i][0], 1)
            EndIf
            _GUICtrlListView_EndUpdate($list2)
        EndIf

    Next
    if $index_old <> -1 Then
    ;_GUICtrlListBox_ClickItem($list2, $index_old)
    _GUICtrlListView_SetHotItem($list2, $index_old)
    EndIf
    ConsoleWrite(@CRLF)

EndFunc

 

The helpfile contains invalid description for _GUICtrlListView_AddItem() it says it returns an index but it really returns an ID

Link to comment
Share on other sites

I found out the problem I'm having is that the listview automatically assigns the new string to the last index of the listview, so as I delete and re-add values the value of the current listview is always the same... any workarounds for this?

Link to comment
Share on other sites

Fixed this problem by using _GUICtrlListView_MapIndexToID() and _GUICtrlListView_MapIDToIndex() interchangeably

Func _getcontacts()

        $oHTTP.Open("GET", "https://pdglobal.net?sid=contactslist&session=" & $session, False)
        $oHTTP.Send()
        $contacts = $oHTTP.ResponseText
    $clist = StringSplit(StringTrimLeft($contacts, 1), "|")
    $index = 0

    _GUICtrlListBox_ResetContent($List2)
    For $i = 1 To $clist[0]
        If $clist[$i] <> "" Then
            $conversations[0][0] += 1
            $conversations[$i][0] = $clist[$i]
            $conversations[$i][2] = _bang(FileRead(IniRead(@ScriptDir & "\settings.ini", "userkeys", $clist[$i], "NA")))
            $conversations[$i][3] = 0
            $conversations[$i][4] = _GUICtrlListView_MapIndexToID($list2,_GUICtrlListView_AddItem($List2, $clist[$i], 1))
            ConsoleWrite($conversations[$i][0]&"="&$conversations[$i][4]&@CRLF)
        EndIf
    Next
EndFunc   ;==>_getcontacts

func _getonlinestatus()
$index_old = _GUICtrlListView_GetHotItem($list2)

    for $i = 1 to $conversations[0][0]
        $oHTTP.Open("GET", "https://pdglobal.net?sid=getonlinestatus&USER="&$conversations[$i][0], False)
        $oHTTP.Send()
        $online = $oHTTP.ResponseText
        ConsoleWrite($conversations[$i][0]&"-"&$online&";")
        if $online = 1 Then
            _GUICtrlListView_BeginUpdate($list2)
            _GUICtrlListView_DeleteItem($list2, _GUICtrlListView_MapIDToIndex($list2, $conversations[$i][4]))
            if $conversations[$i][3] > 0 Then
                $conversations[$i][4] = _GUICtrlListView_MapIndexToID($list2, _GUICtrlListView_AddItem($list2, $conversations[$i][0]&" ("&$conversations[$i][3]&")", 0))
            Else
                $conversations[$i][4] = _GUICtrlListView_MapIndexToID($list2, _GUICtrlListView_AddItem($list2, $conversations[$i][0], 0))
            EndIf
            ConsoleWrite($conversations[$i][0]&"="&$conversations[$i][4]&@CRLF)
            _GUICtrlListView_EndUpdate($list2)
        Else
            _GUICtrlListView_BeginUpdate($list2)
            _GUICtrlListView_DeleteItem($list2,_GUICtrlListView_MapIDToIndex($list2, $conversations[$i][4]))
            if $conversations[$i][3] > 0 Then
                $conversations[$i][4] = _GUICtrlListView_MapIndexToID($list2, _GUICtrlListView_AddItem($list2, $conversations[$i][0]&" ("&$conversations[$i][3]&")", 1))
            Else
                $conversations[$i][4] = _GUICtrlListView_MapIndexToID($list2, _GUICtrlListView_AddItem($list2, $conversations[$i][0], 1))
            EndIf
            ConsoleWrite($conversations[$i][0]&"="&$conversations[$i][4]&@CRLF)
            _GUICtrlListView_EndUpdate($list2)
        EndIf

    Next
    if $index_old <> -1 Then
    ;_GUICtrlListBox_ClickItem($list2, $index_old)
    _GUICtrlListView_SetHotItem($list2, $index_old)
    EndIf
    ConsoleWrite(@CRLF)

EndFunc

 

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