Sign in to follow this  
Followers 0
spudw2k

_GUICtrlListBox_RemoveDuplicates

4 posts in this topic

#1 ·  Posted (edited)

Here is a simple function that...I bet you can guess what it does. :P

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlListBox_RemoveDuplicates
; Description ...: Removes duplicate items in a Listbox control
; Syntax.........: _GUICtrlListBox_RemoveDuplicates($hWnd)
; Parameters ....: $hWnd - Handle or ControlID of the Listbox control
; Return values .: Success - 1, Listbox Cleared and Repopulated with Unique Items
;                  Failure - 0, sets @error to:
;                  |1 - $hWnd parameter is not a valid Listbox ControlID or Handle
;                  |2 - Listbox control has no content
;                  |3 - No duplicate items in Listbox control
; Author ........: Spudw2k
; Modified.......:
; Remarks .......: This function has a dependancy on Array.au3 as it utilizes _ArrayUnique
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _GUICtrlListBox_RemoveDuplicates($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    If Not $hWnd Then Return SetError(1, 0, 0)

    Local $iItemCount = _GUICtrlListBox_GetCount($hWnd)
    If $iItemCount = -1 Then Return SetError(2, 0, 0)

    Local $aItems[$iItemCount]
    For $idx = 0 To $iItemCount - 1
        $aItems[$idx] = _GUICtrlListBox_GetText($hWnd, $idx)
    Next

    Local $aUniqueItems = _ArrayUnique($aItems)
    If UBound($aItems) <= UBound($aUniqueItems) Then Return SetError(3, 0, 0)
    $aItems = $aUniqueItems

    _GUICtrlListBox_BeginUpdate($hWnd)

    _GUICtrlListBox_ResetContent($hWnd)

    For $idx = 1 To $aItems[0]
        _GUICtrlListBox_AddString($hWnd, $aItems[$idx])
    Next

    _GUICtrlListBox_EndUpdate($hWnd)

    Return 1
EndFunc

Demo

#include <GuiListBox.au3>
#include <Array.au3>

$GUI = GUICreate("ListBox_RemoveDuplicates", 200, 320)
$hListBox = GUICtrlCreateList("", 0, 0, 198, 278)
Local $aEntries[13] = ["Peanut Butter", "Jelly", "Sourdough", "Pickles", "Mustard", "Bacon", "Wheat", "Bologna", "Turkey", "Lettuce", "Onion", "Tomato", "Rye"]
For $x = 0 to 10000
    _GUICtrlListBox_AddString($hListBox, $aEntries[Random(0, 12, 1)])
Next
$hButton = GUICtrlCreateButton("Remove Duplicates", 36, 280, 130, 20)

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()

    If $msg = -3 Then ExitLoop

    If $msg = $hButton Then _GUICtrlListBox_RemoveDuplicates($hListBox)
WEnd


; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlListBox_RemoveDuplicates
; Description ...: Removes duplicate items in a Listbox control
; Syntax.........: _GUICtrlListBox_RemoveDuplicates($hWnd)
; Parameters ....: $hWnd - Handle or ControlID of the Listbox control
; Return values .: Success - 1, Listbox Cleared and Repopulated with Unique Items
;                  Failure - 0, sets @error to:
;                  |1 - $hWnd parameter is not a valid Listbox ControlID or Handle
;                  |2 - Listbox control has no content
;                  |3 - No duplicate items in Listbox control
; Author ........: Spudw2k
; Modified.......:
; Remarks .......: This function has a dependancy on Array.au3 as it utilizes _ArrayUnique
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _GUICtrlListBox_RemoveDuplicates($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    If Not $hWnd Then Return SetError(1, 0, 0)

    Local $iItemCount = _GUICtrlListBox_GetCount($hWnd)
    If $iItemCount = -1 Then Return SetError(2, 0, 0)

    Local $aItems[$iItemCount]
    For $idx = 0 To $iItemCount - 1
        $aItems[$idx] = _GUICtrlListBox_GetText($hWnd, $idx)
    Next

    Local $aUniqueItems = _ArrayUnique($aItems)
    If UBound($aItems) <= UBound($aUniqueItems) Then Return SetError(3, 0, 0)
    $aItems = $aUniqueItems

    _GUICtrlListBox_BeginUpdate($hWnd)

    _GUICtrlListBox_ResetContent($hWnd)

    For $idx = 1 To $aItems[0]
        _GUICtrlListBox_AddString($hWnd, $aItems[$idx])
    Next

    _GUICtrlListBox_EndUpdate($hWnd)

    Return 1
EndFunc

edit: Fixed bug - If no duplicates were found the function exited without calling _GUICtrlListBox_EndUpdate()

Moved _GUICtrlListBox_BeginUpdate() after _ArrayUnique comparison.

Edited by spudw2k

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Nice.  ^_^

Another idea, I'm sure you could make a function similar for ListView duplicates.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

I probably could, but that potentially could be a whole other beast. I'm thinking about listview column complications and how I would want to handle it. Remove rows based on duplicates within the a select column or columns.  Could be a fun exercise though.

Share this post


Link to post
Share on other sites

I agree. Some standard would have to be made determining columns, rows, and the entire Listview.

Could be fun, though like you said. :)


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

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